Mohammad abumazen
Mohammad abumazen

Reputation: 1286

Android Run another application while task lock

I set my application as a device owner and the screen is pinning when i call startLockTask() . my problem now when i try to run another application by using this method :

Intent i = getPackageManager().getLaunchIntentForPackage("com.example.test");
startActivityForResult(i,Intent.FLAG_ACTIVITY_NEW_TASK);

(nothing happen) what i have to do to make it run?

Edit : i tried adding

 dpm.setLockTaskPackages(deviceAdmin, new String[] { getPackageName() ,"com.example.test"});

its not launching too.

Upvotes: 6

Views: 3939

Answers (3)

werux
werux

Reputation: 121

A DPC must whitelist apps before they can be used in lock task mode. Call DevicePolicyManager.setLockTaskPackages() to whitelist apps for lock task mode as shown in the following sample:

// Whitelist two apps.

private static final String KIOSK_PACKAGE = "com.example.kiosk";

private static final String PLAYER_PACKAGE = "com.example.player";

private static final String[] APP_PACKAGES = {KIOSK_PACKAGE, PLAYER_PACKAGE};
// ...

Context context = getContext();

DevicePolicyManager dpm =
    (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminName = getComponentName(context);
dpm.setLockTaskPackages(adminName, APP_PACKAGES);

you can declare in your app manifest file how an activity should behave when the system is running in lock task mode. To have the system automatically run your activity in lock task mode, set the android:lockTaskMode attribute to if_whitelisted

android:lockTaskMode="if_whitelisted">

taken from https://developer.android.com/work/dpc/dedicated-devices/lock-task-mode#java

Upvotes: 1

Maddy
Maddy

Reputation: 326

Just try with startActivity when you are launching the app with FLAG_ACTIVITY_NEW_TASK.

Intent i = getPackageManager().getLaunchIntentForPackage("com.example.test");
startActivity(i);

We shouldn't use FLAG_ACTIVITY_NEW_TASK with startActivityForResult in lock task mode.

Upvotes: 0

Misagh
Misagh

Reputation: 3613

You should check the app with an applicationId is installed on the device. for example in your case the applicationId is com.example.test. If the app was not installed, you can bring user to a market or let them choose an app.

String packageName = "com.example.test";
.
.
.
Intent i = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (i == null) {
    i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse("market://details?id=" + packageName));
    // Open app in google play store: 
    // i.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName));
}
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Upvotes: 1

Related Questions