Skip to content Skip to sidebar Skip to footer

How Can I Display The Activity When Device Is Locked?

I want to display one Activity Android, also when I lock my device. Every Time, if I lock my tablet (for example), I should to see one Activity. So, I have built this in AndroidMan

Solution 1:

use this code in last of method onCreate

//show if unlockfinalWindowwin= getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

Solution 2:

Don't use an activity. Android will not show the lock screen behind your activity for security reason, so use service instead of Activity.

Below is my code in onStartCommand of my service.

WindowManagermWindowManager= (WindowManager) getSystemService(WINDOW_SERVICE);

ViewmView= mInflater.inflate(R.layout.score, null);

WindowManager.LayoutParamsmLayoutParams=newWindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);

mWindowManager.addView(mView, mLayoutParams);

And add uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" to manifest

This is copy from another similar post. I tried it real quick with a custom view and it worked well enough. Don't know what your intentions are for it though.

Post a Comment for "How Can I Display The Activity When Device Is Locked?"