简体   繁体   中英

How do i get the launch intent for my work profile app from my app in the primary profile?

This question targets Android for work.

I have written an app including a DevicePolicyController following this guide . The controller creates a managed profile for my Android device.

I added an app to that profile which is called FooApp . Now I can see two icons of FooApp in my launcher, one with work badge and one without.

I also have an app BarApp , which is only in the primary profile. How do I get the launch intent for the work version of FooApp in BarApp ? (Start the work version of FooApp from within BarApp )

I can't use CrossProfileApps as I am on API 23.

The Solution I came up with cannot happen without the users consent. Maybe there will be a better one in the future. But here is mine:

I added the following intent filter for FooActivity in the Manifest of FooApp .

<intent-filter>
    <action android:name="com.example.FooApp.ACTION_HOMELAUNCHER"/>
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Then I added a crossProfileIntentfilter for my work profile (put the following code in the profile administration app currently running in the work profile).

DevicePolicyManager manager =
            (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName componentName = BasicDeviceAdminReceiver.getComponentName(context);
IntentFilter filterin = new IntentFilter("com.example.FooApp.ACTION_HOMELAUNCHER");
manager.addCrossProfileIntentFilter(componentName, filterin, DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

In the end, when I want to launch the App in the work profile, I fire the ACTION_HOMELAUNCHER intent from BarApp and then the user can select, whether he wants to lauch the work profile version or the normal version of FooApp .

Feel free to improve upon this answer.

Since android 9 there is a set of APIs that allow you to switch from personal to work mode within the APP itself. It is not quite what you are trying to achieve but it will improve your solution .. at least for those on android 9 + https://developer.android.com/work/versions/android-9.0#switch-apps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM