How to turn off screen display without restarting

I am running Android Activity

and using

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

      

to turn on the screen.

Later, I cleared the flag with

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

      

But this did not take effect immediately. If I pause the app and resume it, it works.

So this is how I set the flags and start the activity. This is the phonegap app for cordova. In SipAudioCall's onRinging listener:

Activity activity = SIP.this.cordova.getActivity();
  if (activity instanceof MonmouthTelecom) {
    if (((MonmouthTelecom) activity).isActivityPaused()) {
      Intent notifIntent = new Intent(SIP.this.cordova.getActivity().getApplicationContext(),
             Class.forName(SIP.this.cordova.getActivity().getComponentName().getClassName()));
      notifIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Log.i(LOG_TAG, "opening app...");

      // set window flags here b4 starting activity...
      ((MonmouthTelecom) activity).setWindowFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            SIP.this.cordova.getActivity().getApplicationContext().startActivity(notifIntent);
      }
}

      

In the onCallEstablished event handler, I call:

  ((MonmouthTelecom) activity).clearWindowFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

      

set and clear window flags: (when detecting activity)

public void setWindowFlags(int flags) {
this.getWindow().addFlags(flags);
}
public void clearWindowFlags(int flags) {
this.getWindow().clearFlags(flags);
}

      

+3


source to share


1 answer


Ok I figured it out. Apparently I was manipulating the views in different threads and with cordova setup, no exception was thrown.



Calling the add / clear flags in the runnable under SIP.this.cordova.getActivity().runOnUiThread(new Runnable() {...});

made it work.

+1


source







All Articles