6

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.

Mohammad abumazen
  • 1,286
  • 1
  • 11
  • 24

3 Answers3

1

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);
Misagh
  • 3,403
  • 1
  • 20
  • 17
  • Hi @Misagh, Can you able to solve related to like this issue which i'm facing Link-> https://stackoverflow.com/questions/51190997/open-other-application-in-startlocktask-mode-using-setlocktaskpackages-string Thank you. – SaravanaRaja Jul 05 '18 at 12:39
1

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

David Buck
  • 3,752
  • 35
  • 31
  • 35
werux
  • 91
  • 5
0

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.

Maddy
  • 316
  • 1
  • 6
  • 14