I have an application which is doing its work only when devices screen goes off. I have set up broadcast receiver (Intent.ACTION_SCREEN_OFF) and it works fine. I got record in my LOGCAT always when devices screen goes off. So far so good.
My onReceive code from ACTION_SCREEN_OFF uses some code calculating stuff, and all executes fine (When screen goes off). So far so good.
At the end of my onReceive code i'm starting new activity, but the onCreate of targeting activity is NOT always executing. (For example on my HTC Desire 2.3.7 works fine. On HTC Wildfire S 2.3.5, Xperia Arc S 2.3.4 it does not execute, and on Samsung Galaxy ACE 2.2.1 , it depends. Sometimes is executing sometimes isn't). My LOGCAT shows that onReceive executed till the end but Activity was not started. I'm using following code to starting this activity:
Intent startH_Activity = new Intent(getApplicationContext(), HandleActivity.class);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startH_Activity);
The important things to note:
I said that on some phones mentioned above it doesn't work. Well its not working immediately, like it should (when SCREEN_OFF fires ). Always activity starts like.. 10-15 minutes after screen went off (which is not acceptable)
When screen goes off and activity does not start , if I press POWER button on device it immediatelyfires off my target activity and app is working like normal. (again this is not acceptable. It should fire automatically).
When ANY device is connected to the PC it works like it should. Activity starts immediatelyafter screen goes off.
After reading a lot of Stack Overflow I realized that this is because MAYBE device goes to sleep. That's why it works like normal if i press POWER button, or Wakes up automatically after 10-15min because it synchronizes or something. So I used WAKELOCK.
//OnCreate Service
powMan = (PowerManager) getSystemService(POWER_SERVICE);
wake = powMan.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
//OnReceive (screen goes off)
wake.acquire();
But still without a success. (Am i doing wrong?) Even wake lock is acquired my activity won't show up on these devices.
Basically what I'm asking. How do i open usual activity when screen goes off? Or at least how to turn screen ON if activity won't start (remember, pressing on power button shows up my activity
I would really need some help now, because I'm getting really frustrated about this. Thank you for help.
You should be starting Service instead of Activity when screen goes off.
I would register yet another broadcast receiver and start the Activity when screen goes on (Intent.ACTION_SCREEN_ON).
Two issues:
1) How do you pass data from one receiver to another? What comes to my mind is either starting a Service or saving the information to a file (or shared preferences).
2) What happens if you have two events that must start an Activity? (E.g. "something started" and "something finished".) You must resolve this, either showing both events or just the latest one.
Note that the user can turn the screen on and off without unlocking the screen (Intent.ACTION_USER_PRESENT), and that if the phone is configured not to lock the screen, Intent.ACTION_USER_PRESENT does not happen.
Related
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.
I have been struggling with this bug for days. This bug happens under rare but likely condition and I just cannot find why this is happening.
What happens is:
In split screen mode (my app at top and another at bottom), when certain activity quits, back-stack activity's (in my case, mostly MainActivity) onStart() is never called, and split screen mode looks like it's ended because my app is dead, but the overview button still looks like I'm in split screen mode and when I tap the split screen button, my MainActivity's onStart() is finally called, and it's back to split screen mode again.
The log looks like
MainActivity onStop()
ProblematicActivity onCreate()
...
ProblematicActivity onDestroy()
//and nothing!
This does not happen under these conditions:
When the problematic activity is called in not-split-screen mode, but finishes in split-screen mode. (both resumed and paused)
When the problematic activity is called in split-screen mode, but my app is tapped (i.e, my activity is in resumed state)
This happens only when:
Problematic activity is started in split-screen mode, and my activity is not tapped (i.e., my activity is in paused state)
I've checked activity lifecycle callback thoroughly (at least I think so) and looked into logs many times but I just cannot figure out why BackStack activity is not called.
I've tried searching but there were no much helpful information. Did anyone had any experience/problem similar to mine?
Thanks.
additional Info
My activity stack looks like this
MainActivity
Activity 2
Activity 1
Fragment 1
Server sends my app to start a feature, then it starts Activity 2, it starts activity 1, and Fragment 1 is in activity 1 (what user really sees is fragment 1) Activity & fragments are well-destroyed until Activity2. But From there, MainActivity (or no matter what activity it is) is not visible since AndroidSystem never calls onStart.
As it turns out it was a defect in Android OS.
Google's response is as below
Hi,
We have passed this defect on to the development team and will update this issue with more information as it becomes available.
Thanks
Issue link:
https://code.google.com/p/android/issues/detail?id=231337&can=1&q=reporter%3Aadobecs5&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened
I'm trying to find out what the difference is in the activity lifecycle when a lock-screen is enabled versus disabled.
Scenario A (no lock-screen)
App is running
power button is hit to turn display off
power button is hit to turn display on
App is immediately resumed
Scenario B (lock screen enabled, swipe to unlock)
App is running
power button is hit to turn display off
power button is hit to turn display on
Swipe screen to unlock
App is resumed (but we are getting a bug in the display, which is where we are trying to find the difference)
I have printed out the activity lifecycle for both scenarios and they show up identical for both scenarios.
What might be the difference in the lifecycle that would cause this different behavior when using lock-screen versus no lock-screen?
Take a look at this documentation. It could be that the lock screen would imply that the app process is killed and therefore data is lost (which is necessary to present the page and therefore the app crashes). I would suggest to debug the activity states to find the answer you're looking for.
You can test it with writing Logs into onResume(), onCreate(), onPause(), onDestroy() etc.
Also pressing Power button and and opening LockScreen changes the devices screen orientation into "Portrait" mode(Phones and tablets with locked rotation only). It may cause different reactions you are mentioned about.
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
I have an app which uses the ACTION_MEDIA_BUTTON intent with a BroadcastReceiver to control a music player. The user pushes a button on external hardware and it controls the in-app music player. The user can also HOLD DOWN the button and change the volume.
I recently downloaded another app which uses the headset button, and it takes over the media button intent from my app! So when this other app is open and I press the button, the other app will start running, but my app will think that the button is still pressed down so it will cycle the volume.
To summarize,
my app is open, supposed to be sole listener of media button intents
other app gets opened, it also wants to be sole listener of media button intents
button gets pressed with both apps open, control goes to other app
my app thinks the button is being held down, as it lost control as the button was pressed in down mode (I think). It then launches functions I don't want launched because it thinks the user has held down the button.
Is there any way I could make sure that while my app is open it's the sole receiver of this media button intent? Could I at least check to see if another app has taken over, so I can prevent unexpected behaviour?
Thank you for any help, I've never had apps not play nicely before!
You can alter your BroadcastReceiver's priority (make it something large, like 10000): it should then get the Intent first, and then you can pass it on to the other app.
I have a similiar issue. I believe, outside of the 'arms race' over the priorites mentioned, the only real solution is to close the other application. If you are releasing this application to other users, you could possibly give them a message telling them to close other media player apps and services.
In your manifest you can set the intent priority to the max value of an integer which is: 2147483647.
You should not however set your IntentFilter priority over 1000 as it tells you in the API docs. You can set the IntentFilter priority like so:
myIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
I assume you should subtract one from the SYSTEM_HIGH_PRIORITY constant as the docs say the value must be less than SYSTEM_HIGH_PRIORITY.
Quote from docs about IntentFilter.setPriority(int):
Applications must use a value that is larger than SYSTEM_LOW_PRIORITY and smaller than SYSTEM_HIGH_PRIORITY.