Screen pinning 3rd party apps programmatically
O

3

18

After achieving device ownership, I am trying to implement a method to instruct the device to lock any given app into kiosk mode (or screen pinning mode). Since I have device ownership, the user is not asked for the permission to do so.

From the developer website, brief description tells me that it is possible to do what I am trying:

http://developer.android.com/about/versions/android-5.0.html#ScreenPinning

Programmatically: To activate screen pinning programmatically, call startLockTask() from your app. If the requesting app is not a device owner, the user is prompted for confirmation. A device owner app can call the setLockTaskPackages() method to enable apps to be pinnable without the user confirmation step.

This indicates that as a device owner app, I can pin other apps without user confirmation... but I have no idea how to.

I have been able to put my own app into pinned mode.

Any help would be appreciated.

Omeara answered 29/1, 2015 at 0:6 Comment(2)
Are the apps you want to lock yours or 3rd-party apps?Schafer
@Schafer , The app I want to lock are 3rd party apps.Omeara
K
18

The setLockTaskPackages() is used the specify which applications (through their package names) will be able to programmatically be pinned without user confirmation. The setLockTaskPackages() is called from your device owner app (most probably in your DeviceAdminReceiver's onEnabled() method).

So, in you owner device app, you'll have something like :

mDPM.setLockTaskPackages("com.foo.myapp");

and then, in your "com.foo.myapp" application, you will be autorized to call :

startLockTask(); 

Your application will immediately enter the Pinning mode, without any user confirmation.

If you don't first register your application with setLockTaskPackages, the application will be pinned but the user will have to confirm first.

Also notice that when an app is registered with setLockTaskPackages(), it has some different behaviours than the manual pin:

  • the user cannot unpin manually the application by long-pressing Back + Recent Apps. You'll have to programmatically unpin your app with stopLockTask();
  • The "Home" and "Recent Apps" buttons are invisible (not displayed)
  • When the app is unpinned (via stopLockTask()), the user will directly go back to Home : no Screen lock is displayed, even if a Keyguard is set (Pattern, code, or whatever Keyguard screen).
Keri answered 29/1, 2015 at 11:3 Comment(13)
can we pinned other app using setLockTaskPackages method?Formula
@FlorentDupont, does the app you gave for example com.foo.myapp has to be an app published by you which has to authorize screen pinning? Can it be that I can lock, say angry birds... com.rovio.angrybirds?Omeara
@suniljain : no you can't pin another app with setLockTaskPackages. You'll have to use the other app's Activity.startLockTask() to do that. @Omeara yes, in this example, com.foo.myapp has to be published by me. You cannot programmatically lock another app that you don't own.Keri
@FlorentDupont according to this: public void setLockTaskPackages (ComponentName admin, String[] packages), "Any packages that shares uid with an allowed package will also be allowed to activate lock task." It seems that the logic in creating screen pinning api was to allow device owner to lock other apps. But I think we are missing some method.Omeara
I wrote a complete article on the subject. available here : florent-dupont.blogspot.fr/2015/02/…. I hope it will answer your questions.Keri
@FlorentDupont thats a really good article... very comprehensive. Thanks for all your help.Omeara
@FlorentDupont i wrote an application that is owner and administrator, i can successfully pin the application without user confirmation but the user is still able to unpin the application long-pressing Back + Recent Apps, the buttons are still there because they are physical buttons, my device is Samsung Galaxy Tab a. Maybe a wrong implementation of by Samsung.Cheshire
yes I know some vendors don't correctly implement this. It'd recommend using the official Google phones/tables. I tested on them and it works fine.Keri
@jogrimst I have my app as device owner. The purpose of asking this question was trying to lock 3rd party apps programatically without showing them a pop up notification.Omeara
can I use ths method to remedy the absence of screen pinning on my huawei p9 lite? e.g. I'd like to pin youtube or chromeTyronetyrosinase
Hello all please help me.. how can i create device owner app i have try many ways but i am failed.. thanks..Memphian
It seems there is other ways of screen pinning. App like Touch Lock Lite achieved this without DeviceArministrator. Can someone share a hint?Greenman
@FlorentDupont Thanks for the article! I am unable to receive "onLockTaskModeEntering" intent. I have added the intent filter to my receiver in the manifest. In the documentation it says the app has to be a device owner. Is this new? Is it still possible to receive this intent?Bastinado
A
4

I've not enough reputation for a comment, just would point out that for devices with physical buttons (like the Samsung Galaxy Tab A mentioned by @chairman) one way for manage the forced unpinning of your application is to implement in your DeviceAdminReceiver class the following:

@Override public void onLockTaskModeExiting(Context context, Intent intent)

So if your user want to for the unpin you can always re-pinning your app ;)

Avail answered 17/2, 2016 at 8:5 Comment(0)
M
0

Here's a code snippet that should get you going:

DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(this, DeviceAdminSample.class);

if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
    // Device owner
    String[] packages = {this.getPackageName()};
    myDevicePolicyManager.setLockTaskPackages(mDeviceAdminSample, packages);
} else {
    // Not a device owner - prompt user or show error
}

if (myDevicePolicyManager.isLockTaskPermitted(this.getPackageName())) {
    // Lock allowed
    startLockTask();
} else {
    // Lock not allowed - show error or something useful here
}
Monson answered 29/1, 2015 at 0:14 Comment(5)
by this we can only pinned own app but as kash explained that he wants to pinned other app, is it possible using DevicePolicyManager with Device Owner Privileges?Formula
Any way to pin other applications from our app. As we know that the home and recent buttons will be invisible. Then how is it possible to got to some other application to pin it.Basically the requirement is to be able to use the device in kiosk/pinned mode for more than one application.Quincunx
@Quincunx You basically need to add the package name of your application in the device owner. Then from your app you call the startLockTask(). This is the only method I found so far.Omeara
where to find DeviceAdminSample.class ?Wearable
@KalpeshKulye that is just a in manifest registed DeviceAdminReceiver. Here is an example when you want to be an device owner: github.com/googlearchive/android-DeviceOwner/blob/master/…Pelag

© 2022 - 2024 — McMap. All rights reserved.