How to check if the current task is locked?

I play with the task locking API on Nexus 4 running Android 5.0.1. The application of my device owner consists of two buttons: “Lock” and “Unlock”, which simply call startLockTask() and stopLockTask() . This is really all that is needed, but as some people insist on seeing the template:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.lockButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startLockTask(); } }); findViewById(R.id.unlockButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopLockTask(); } }); } } 

Pressing the Lock button when a task is already locked is harmless. But if I press the "Unblock" button when the task is not locked, I get NPE:

 02-12 22:58:11.942: E/AndroidRuntime(12888): java.lang.NullPointerException: Attempt to read from field 'android.content.Intent com.android.server.am.TaskRecord.intent' on a null object reference 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.os.Parcel.readException(Parcel.java:1546) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.os.Parcel.readException(Parcel.java:1493) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.app.ActivityManagerProxy.stopLockTaskMode(ActivityManagerNative.java:5245) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.app.Activity.stopLockTask(Activity.java:6179) 02-12 22:58:11.942: E/AndroidRuntime(12888): at com.chalcodes.kiosk.MainActivity$2.onClick(MainActivity.java:44) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.view.View.performClick(View.java:4756) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.view.View$PerformClick.run(View.java:19749) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.os.Handler.handleCallback(Handler.java:739) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.os.Handler.dispatchMessage(Handler.java:95) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.os.Looper.loop(Looper.java:135) 02-12 22:58:11.942: E/AndroidRuntime(12888): at android.app.ActivityThread.main(ActivityThread.java:5221) 02-12 22:58:11.942: E/AndroidRuntime(12888): at java.lang.reflect.Method.invoke(Native Method) 02-12 22:58:11.942: E/AndroidRuntime(12888): at java.lang.reflect.Method.invoke(Method.java:372) 02-12 22:58:11.942: E/AndroidRuntime(12888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 02-12 22:58:11.942: E/AndroidRuntime(12888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

Is there a way somewhere to check if the current task is locked?

This is similar to how unregisterReceiver (...) explodes if you try to unregister a recipient that is not registered. I think this is a bad API that makes you keep track of its internal state. But stopLockTask() even worse, because while you usually unregister your receivers when your activity is paused, you usually will not unlock your task. So, the next action is created blocked, but he does not know that it is blocked. Thus, you basically need to pass the locked state in intent and saved instance packages. Or just surround every stopLockTask() call with an ugly try / catch ...

Do I make sense here?

Edit: Created issue # 150089 .

+8
android android-screen-pinning
source share
2 answers

Here is a method to check the current state of the current task if it is locked or not in case of screen binding:

 !mActivityManager.isInLockTaskMode() 

For more information, follow this link: http://developer.android.com/reference/android/app/ActivityManager.html#isInLockTaskMode%28%29

+5
source share

You need to implement other lifecycle methods (OnResume, OnStop, etc.).

Put your logic in to unlock the application in another method in which you find out which one is better according to your expected behavior. Check it out [link]. one

-2
source share

All Articles