简体   繁体   English

屏幕以编程方式固定第三方应用

[英]Screen pinning 3rd party apps programmatically

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 http://developer.android.com/about/versions/android-5.0.html#ScreenPinning

Programmatically: To activate screen pinning programmatically, call startLockTask() from your app. 以编程方式:要以编程方式激活屏幕固定,请从您的应用程序调用startLockTask()。 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. 设备所有者应用程序可以调用setLockTaskPackages()方法,以便在没有用户确认步骤的情况下使应用程序可以固定。

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. 任何帮助,将不胜感激。

The setLockTaskPackages() is used the specify which applications (through their package names) will be able to programmatically be pinned without user confirmation . setLockTaskPackages()用于指定哪些应用程序(通过其包名称)能够以编程方式固定而无需用户确认 The setLockTaskPackages() is called from your device owner app (most probably in your DeviceAdminReceiver 's onEnabled() method). setLockTaskPackages()是从您的设备所有者应用程序 DeviceAdminReceiver的(很可能是在您的DeviceAdminReceiveronEnabled()方法中)。

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 : 然后,在您的“com.foo.myapp”应用程序中,您将被自动调用以调用:

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. 如果您没有首先使用setLockTaskPackages register您的应用程序,则应用程序将被固定,但用户必须先确认。

Also notice that when an app is registered with setLockTaskPackages() , it has some different behaviours than the manual pin : 另请注意,当应用程序使用setLockTaskPackages() registered时,它具有一些与手动引脚不同的行为:

  • the user cannot unpin manually the application by long-pressing Back + Recent Apps . 用户无法通过长按Back + Recent Apps手动取消固定应用程序 You'll have to programmatically unpin your app with stopLockTask() ; 您必须使用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). 取消固定应用程序后(通过stopLockTask()),用户将直接返回主页:即使设置了键盘锁(图案,代码或任何键盘锁定屏幕),也不会显示屏幕锁定。

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: 我没有足够的评论声誉,只是会指出对于带有物理按钮的设备(如@chairman提到的三星Galaxy Tab A),管理应用程序强制取消固定的一种方法是在DeviceAdminReceiver类中实现以下:

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

So if your user want to for the unpin you can always re-pinning your app ;) 因此,如果您的用户想要取消固定,您可以随时重新固定您的应用;)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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