如何使我的安卓应用程序成为设备管理员?

15
我正在尝试使我的应用程序成为设备管理员,我按照设备管理示例说明此处的教程提供的说明进行操作,但仍然无法实现。
请问有没有可行的示例能够指导我完成这个过程,我只需要确保如果用户要卸载该应用程序,则需要输入在我的应用程序中设置的密码。
感谢任何帮助。

如果你不允许删除它,那么这听起来更像是恶意软件而不是有用的应用程序,对吧? - banzai86
2
基本上像家长控制一样,这样孩子就不能删除应用程序,除非父母插入密码。 - rolling.stones
3个回答

8

我只需要确保用户在卸载该应用程序时需要输入在我的应用程序中设置的密码。

幸运的是,对于 SDK 应用程序来说,这是不可能实现的。即使是设备管理器应用程序也无法防止用户卸载它。


@CommonsWare:您提到对于SDK应用程序不可能。那么,系统应用程序是否可以实现呢?我能否创建一个受密码保护且用户无法卸载的系统应用程序? - user1010
@user1010:如果是系统应用程序,用户无法卸载它,除非他们拥有 root 权限。 - CommonsWare
哦,是的,我怎么能忘记呢。 - user1010
@CommonsWare:我认为这不是安全漏洞。除非取消设备管理员权限,否则无法卸载已激活的设备管理应用程序。 - Kiran Parmar
@Jolinar:Android SDK中没有任何支持此功能的内容。恶意软件作者找到了一些绕过此限制的方法,但这些方法在较新版本的Android中已被阻止。 - CommonsWare
显示剩余3条评论

3
我在这里找到了一个示例教程(链接),希望它能对你有所帮助。 警告:如果您点击“重置设备”,您的设备将被恢复出厂设置。
devicePolicyManager.wipeData(ACTIVATION_REQUEST);

1

您无法通过编程方式激活它,但您可以要求用户在运行时启用它。以下是完整的解决方案:

 var devicePolicyManager: DevicePolicyManager? = null
    var deviceAdmin: ComponentName? = null

在需要的地方,在oncreate中调用此方法。

// 调用此方法以检查它是否已启用

checkAndTakeUserToEnableEnableAdminApp()



private fun checkAndTakeUserToEnableEnableAdminApp() {
        devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        deviceAdmin = ComponentName(this, DeviceAdminReceiverClass::class.java)
        if (devicePolicyManager!!.isAdminActive(deviceAdmin!!)) {
        //do whatever is needed here is its active
        } else {
            showDeviceAdminPopup(this)
        }

    }

在哪里 DeviceAdminReceiverClass

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;



/**
 * This is the component that is responsible for actual device administration.
 * It becomes the receiver when a policy is applied. It is important that we
 * subclass DeviceAdminReceiverClass class here and to implement its only required
 * method onEnabled().
 */
public class DeviceAdminReceiverClass extends DeviceAdminReceiver {
    static final String TAG = "DeviceAdminReceiverClass";

    /** Called when this application is approved to be a device administrator. */
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Toast.makeText(context, "Device admin is enabled",
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onEnabled");
    }

    /** Called when this application is no longer the device administrator. */
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, "Device admin is disabled",
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.d(TAG, "onPasswordChanged");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.d(TAG, "onPasswordFailed");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.d(TAG, "onPasswordSucceeded");
    }

}

清单文件
<receiver
            android:name=".DeviceAdminReceiverClass"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

@xml/device_admin

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<device-admin xmlns:android="http://schemas.android.com/apk/res/android"
    android:visible="false">
    <uses-policies>
        <watch-login />
        <force-lock />
    </uses-policies>
</device-admin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接