Android pin activity on boot

后端 未结 3 1984
一整个雨季
一整个雨季 2021-02-14 17:05

I\'ve got an app that registers itself as the default launcher and pins itself automatically when started.

This all works fine when installing the app. It pins itself a

相关标签:
3条回答
  • 2021-02-14 17:19

    The only solution I found as for now : make another launcher app, without locktask, which will trigger main app every time when launcher appears. This prevent user for waiting few more seconds before LockTasked app is being called with on BOOT_COMPLETED receiver. So we can meet this problem only when lockTask app has launcher properties for some activity in manifest.

    0 讨论(0)
  • 2021-02-14 17:21

    Sorry for late answering, but...
    Anyone has this problem can do this tricky work in first (LAUNCHER/HOME) activity (e.g. MainActivity):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (mSharedPreferences.getBoolean(KEY_PREF_RECREATED, false)) {
            mSharedPreferences.edit().putBoolean(KEY_PREF_RECREATED, false).apply();
    
            // start LOCK TASK here
        } else {
            mSharedPreferences.edit().putBoolean(KEY_PREF_RECREATED, true).apply();
    
            finish(); // close the app
            startActivity(new Intent(this, MainActivity.class)); // reopen the app
            return;
        }
    
        setContentView(R.layout.activity_main);
    
        // other codes
    }
    
    0 讨论(0)
  • 2021-02-14 17:41

    I had the same problem and I could really only find one solution. I'm not sure why but yeah, something in android prevents task locking when booting up which boggles my mind since the task lock was designed to create these "kiosk" type of applications. The only solution I could find was to detect for a case when it didn't lock then restart the application. Its a little "hacky" but what else can you do?

    To detect for the case where it didn't lock I created a state variable and assigning states (Locking, Locked, Unlocking, Unlocked). Then in the device admin receiver in onTaskModeExiting if the state isn't "Unlocking" then I know it unlocked on its own. So if this case happened where it failed, I then restart the application using this method (which schedules the application in the alarm manager then kills the application):

    how to programmatically "restart" android app?

    Here is some 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 it should hopefully get you pointed towards a solution.

    0 讨论(0)
提交回复
热议问题