Start another app while task-locking is enabled

人走茶凉 提交于 2020-08-10 04:05:40

问题


I used this guide to activate my app as device owner. So, I can activate task locking. This is very close at how I want Android to behave.

Is it possible to start one or more specific third-party-apps out of the device owning app and without deactivating the task-lock?

If not, is it possible with a little workaround? I am thinking about deactivating the task-lock, starting the other app and then activating task-lock for the other app remotely.

Thank you in advance.


回答1:


A locked task can only launch third-party activities if their launch flags allow them to be launched into the same task. If you try to launch an activity in a new task, it'll print a warning to logcat and the user will see nothing.

AFAIK, there is no general way to lock another task. The other task would have to be designed to lock itself in response to some intent.




回答2:


In my case, my app is an enterprise app that needs to lockdown the device, so the use of kiosk mode. But my app needs to call telephone and Google Maps apps.

Not sure if it is a bug or not, but some versions of Android startActivity() does not work even if you call setLockTaskPackages() correctly. It seems to be a problem with lollipop. To workaround I used startActivityForResult instead.




回答3:


I know I am too late for the party but here is what I did to get it working for me.

When you make your app as device owner you have to call this method:

DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    // get this app package name
    ComponentName mDPM = new ComponentName(this, DeviceAdmin.class);
    Utility.writeLogs(this, getString(R.string.info), "Trying to start lock task...");

    if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
        // get this app package name
        String[] packages = {this.getPackageName()};
        // mDPM is the admin package, and allow the specified packages to lock task
        myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
        startLockTask();
    } else {
        Toast.makeText(getApplicationContext(), R.string.not_owner, Toast.LENGTH_LONG).show();
    }

Just add the package name of the application you want to allow to be opened from you application in

String[] packages = {this.getPackageName(), "Package names to be allowed"};

and it should work for you.



来源:https://stackoverflow.com/questions/32230729/start-another-app-while-task-locking-is-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!