Calling onCreate() from locked screen - java

I am developing an android emergency app and I want to add a feature that can call a certain method from my activity class when the screen is locked. Is there anyway for my app to detect any kind gestures like shake, screen tap pattern or any kind of movement when the screen is locked?

Not sure if it will work, but I think you can put a listener in a service and put the servicee in the foreground. I have no clue what exactly youwant to achieve but this is what I can think of right now. BTW -- pass information from a service the foreground activity

Related

Interfacing with MainActivity from video player

I'm new to android app development and just creating a simple application for my workplace that allows video playback control from a web panel, so the app pings the panel on a loop and if it gets a request to play a video, it plays it. If it gets a request to stop the video or play something else, then it does that.
From MainActivity I'm using startActivity to start an instance of ExoPlayer, I know how to pass variables to the activity, but how do I send information the other way or control the playback? Basically that loop that runs constantly to check for new actions is running in MainActivity, once the player is started I have no ability to stop playback, get stream metrics or do anything with that instance at all. I realise I'm probably doing things backwards and should have a background service do the checks, but I still have no idea how to pass information back and forth.
Does anyone have any tips or suggestions? Thank you!
Instead of using two activities, you have to use an acitivty (the main) and a fragment (the video player). Then define a callback interface which will implement the activity and thus manage the communication between fragment and activity
See this https://blog.mindorks.com/how-to-communicate-between-fragments/

Android app not restart from mainactivity while after open background

Hello our application works perfect but when our users move the app to background, after a while coming back to app its been crashing.
For example i open the app then change three pages then move the app to background. after open from background 30 minutes its crash because its try to load activity when i resumed
Another applications do this from mainactivity, for example: instagram,twitter vs vs.
another applications not to try load resume activity, they are trying to main activity
how can i start my app from mainactivity when user come back to app from background ?
well, the better way would be to identify the null objects and reassign or repopulate them in the onResume() method so that the user can actually return to what they were doing.
if you can't or it's not an option, then try this code in every activity that you do not want to return to.
override fun onTrimMemory(level: Int) {
this.finishAffinity()
}
this code closes the active activity as soon as the app goes into background so when returned, the app is forced to start from the launcher activity.
Although I am not convinced that this is the best approach. there might be something better.

How to detect light-up unlock and unlock of the screen in background

I am working on an android app trying to record the usage of the phone. For one function, I am trying to detect whether the screen is light up (not necessary to be unlocked), unlock and lock. How can I capture this three kinds of behavior of user?
Thanks!
Register a BroadcastReceiver
Use Intent.ACTION_SCREEN_OFF and ACTION_SCREEN_ON to detect screen on/off event
Guideline about how to use BroadcastReceiver can be found here
https://developer.android.com/guide/components/broadcasts.html

Refresh of screens on backgrounding and foregrounding an application

I am working on an Android app with multiple activities. When moving from one activity to another in certain cases I want to refresh the display but not in others.
One case is where I background the application and foreground it again. When I foreground it, I want to refresh everything on the screen depending on which activity
I backgrounded to begin with. How can I do this? I am unfortunately a bit new to Android so some appropriate basics where applicable would also be helpful.
http://developer.android.com/guide/components/activities.html#SavingActivityState
You can use onSaveInstanceState() to save information before onPause() is called. In onResume() you can use the saved info as a case in a switch statement or some conditional to refresh what you want.

The sound stopped when press lock button

I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.

Categories