Search code examples
androidandroid-activitybroadcastreceiverlockscreenpowermanager

Android activity over lock screen


I need help with my project , I'm supposed to make a BroadcastReceiver fires up a full screen activity over current lock screen . I tried this on my activity :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("onCreate","success");
    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
    wakeLock.acquire(10*60*1000L /*10 minutes*/);
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    this.setShowWhenLocked(true);
    this.setTurnScreenOn(true);
    setContentView(R.layout.activity_stop);
}

BroadcastReceiver:

Intent i = new Intent(context,StopTheAlarm.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

AndroidManifest.xml:

<activity android:name=".Stop"
         android:showOnLockScreen="true"
         android:screenOrientation="sensorPortrait">
</activity>

but it doesn't work on my android 7.0.1 phone


Solution

  • Update : i removed

    this.setShowWhenLocked(true);
    this.setTurnScreenOn(true);
    

    which were causing app to crash