Android pin activity on boot

I have an application that registers as the default launcher and automatically binds itself at startup.

All this works great when installing the application. It is fixed and only the back button is displayed.

The problem is that when the device first boots up, it does not lock properly. Several times I see a series of toasts “Screen pinned” and “Screen unpinned”. The Home and Recent Tasks buttons are still visible.

-

Running the “adb shell dumpsys activity action” - the last lines indicate that it is not pinned:

mLockTaskModeState=NONE mLockTaskPackages (userId:packages)= 0:[com.example.myapp] mLockTaskModeTasks[] 

-

Asus ZenPad Testing Device Works with Marshmallow / 6.0 / 23

I rely on the MainActivity attribute attribute "lockTaskMode" on the output (and not on activity.startLockTask ()):

 <activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:label="@string/launcher_main" android:launchMode="singleTask" android:lockTaskMode="if_whitelisted" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.HOME"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> 

Any help or pointers would be appreciated

+7
android android-launcher device-owner
source share
2 answers

I had the same problem and I really could only find one solution. I’m not sure why, but yes, something in the android prevents the task blocking at boot, which makes me wise, because the task block was designed to create these kiosk-like applications. The only solution I could find was to detect the case when it is not locked, and then restart the application. Its a bit “hacked,” but what else can you do?

To detect a case when it is not locked, I created a state variable and assigning states (lock, lock, unlock, unlock). Then in the device administrator's receiver in onTaskModeExiting, if the state is not “unlock”, then I know that it is unlocked by itself. Therefore, if this incident occurred where it failed, I restarted the application using this method (which plans the application in the alarm manager then kills the application):

how to programmatically restart the "Android app?

Here is a sample code:

DeviceAdminReceiver

 @Override public void onLockTaskModeEntering(Context context, Intent intent, String pkg) { super.onLockTaskModeEntering(context, intent, pkg); Lockdown.LockState = Lockdown.LOCK_STATE_LOCKED; } @Override public void onLockTaskModeExiting(Context context, Intent intent) { super.onLockTaskModeExiting(context, intent); if (Lockdown.LockState != Lockdown.LOCK_STATE_UNLOCKING) { MainActivity.restartActivity(context); } Lockdown.LockState = Lockdown.LOCK_STATE_UNLOCKED; } 

Mainactivity

 public static void restartActivity(Context context) { if (context != null) { PackageManager pm = context.getPackageManager(); if (pm != null) { Intent intent = pm.getLaunchIntentForPackage(context.getPackageName()); if (intent != null) { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); int pendingIntentId = 223344; PendingIntent pendingIntent = PendingIntent.getActivity(context, pendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent); System.exit(0); } } } } private void lock() { Lockdown.LockState = Lockdown.LOCK_STATE_LOCKING; startLockTask(); } private void unlock() { ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED) { Lockdown.LockState = Lockdown.LOCK_STATE_UNLOCKING; stopLockTask(); } } 

In truth, this is a simplified version of what I implemented. But we hope you point to a solution.

+2
source share

The only solution I found now: make another application to run, without locktask, which will launch the main application every time the launch bar appears. This prevents the user from waiting a few seconds before the LockTasked application is called with the BOOT_COMPLETED receiver. Thus, we can meet this problem only when the lockTask application has launch properties for some activity in the manifest.

0
source share

All Articles