0

While the phone is sleeping I want to have an animated activity (much like the phone ringer animation).

I've read many posts regarding turning the screen on using the WindowManager flags, so what I did was adding this piece of code to my activity's onCreate() function:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(
         WindowManager.LayoutParams.FLAG_FULLSCREEN |
         WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
         WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,

         WindowManager.LayoutParams.FLAG_FULLSCREEN |
         WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
         WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    );

    setContentView(R.layout.act_image_activity);
    startAnimation();
}

My issues are:

  • The animation starts with a small delay; when the screen turns on, I can see the keyguard (or the home screen when the keyguard is disabled), and after that my activity kicks in.
  • After calling my activity's finish() method, the phone doesn't go to sleep right away, rather it starts the sleep timer all over again.

Can someone please tell me how can I get my animated activity to display immediately after the screen turns on, and have the screen turn off immediately after it finishes ?

dror
  • 3,759
  • 6
  • 31
  • 45

2 Answers2

0

But this reciever to your manifest

<receiver android:name="IntentReceiver" >
<intent-filter>
     <action android:name="android.intent.action.SCREEN_ON" ></action>
     <action android:name="android.intent.action.SCREEN_OFF" ></action>
</intent-filter>
</receiver>

 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.activity_main);

       registerReceiver(new BroadcastReceiver() {                           
       public void onReceive(Context arg0, Intent arg1) {
            Log.v("tag", "screen on");
            // You can catch  screen on here and start your animation
         }
       }, new IntentFilter(Intent.ACTION_SCREEN_ON));

       registerReceiver(new BroadcastReceiver() {
       public void onReceive(Context arg0, Intent arg1) {
            Log.v("tag", "screen off");
            // You can catch  screen off here and start your animation
          }
        }, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }
Talha
  • 12,673
  • 5
  • 49
  • 68
0

As for the initial delay and seeing a part of the keyguard, I don't think there's a way to prevent that. However, you can force the screen to turn off by calling PowerManager.goToSleep.

Bruno Oliveira
  • 5,056
  • 18
  • 24
  • 1
    You can't turn off the screen as a normal app since you don't get the required permission: http://stackoverflow.com/questions/5710971/android-what-permissions-required-to-call-powermanager-gotosleepn-put-device-i – zapl Nov 19 '12 at 22:01