简体   繁体   中英

How do i start activity when screen goes off?

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.

屏幕关闭时,您应该启动服务而不是活动。

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? (Eg "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM