Unlock device, display text, then lock again

To use my application, I need to display a message on the screen, even if the screen lock is on, and then wait 3 seconds than I need to lock the phone again, since I do not want it to make unwanted phone calls in your pockets.

The first part is simple:

if (PreferenceManager.getDefaultSharedPreferences( getBaseContext()).getBoolean("wake", false)) { KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode(); WakeLocker.acquire(ProtoBenService.this); Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isKeyguardUp) { ProtoBenService.this.startActivity(myIntent); } else Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show(); WakeLocker.release(); } 

With this class:

 public abstract class WakeLocker { private static PowerManager.WakeLock wakeLock; public static void acquire(Context ctx) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "CobeIm"); wakeLock.acquire(); } public static void release() { if (wakeLock != null) wakeLock.release(); wakeLock = null; } } 

And activity:

 public class LockActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); TextView tv = new TextView(this); tv.setText("This is working!"); tv.setTextSize(45); setContentView(tv); Runnable mRunnable; Handler mHandler = new Handler(); mRunnable = new Runnable() { @Override public void run() { LockActivity.this.finish(); } }; mHandler.postDelayed(mRunnable, 3 * 1000); } } 

So this is good, the phone can display my text!

The only problem occurs when I want to lock the phone again, it seems that the phone lock is protected by the system ...

I think that my users will not understand Device Admin and will not be able to activate it. Is there a way around the screen to lock the screen without using Device Admin files?

+4
source share
2 answers

I'm sure you need to use Device Admin features to lock the screen.

  protected static void initiateDeviceLock(Context context) { ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class); DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); boolean active = dpm.isAdminActive(componentName); Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active)); if (active) { dpm.lockNow(); } } 

To help the user configure the Device Admin functions, you can go to the settings page:

  Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); startActivityForResult(intent, CODE); 
+1
source

I used the following method to lock and unlock the phone.

initialization

  KeyguardLock keyguard; KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); keyguard = km.newKeyguardLock("MyApp"); 

to unlock the phone

 keyguard.disableKeyguard(); 

to lock the phone again

 keyguard.reenableKeyguard(); 

to turn on the screen

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

and do not forget to use the following permission

 <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 
+2
source

Source: https://habr.com/ru/post/1265675/


All Articles