我正在尝试以设备管理员身份启用我的应用程序。尝试调用允许我的应用程序成为我应用程序第一个 Activity 中的设备管理员的 Activity 的代码如下:

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesUtil;

public class RegisterActivity extends Activity {
    // alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();

    // Internet detector
    ConnectionDetector cd;

    // UI elements
    EditText txtName;
    EditText txtEmail;

    // Register button
    Button btnRegister;

    static final int ACTIVATION_REQUEST = 47; //Request ID for Device Administrator

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //Unrelated
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (status != 0) {
            Toast.makeText(this, "This device is not supported - please download Google Play Services.",
                      Toast.LENGTH_LONG).show();
        }

        //Begin enabling device administrator
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        ComponentName deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You must enable device administration for certain features"
                + " of the app to function.");

        //I thought that this would start the activity that lets the user
        //choose whether to enable the app as a device admin
        startActivityForResult(intent, ACTIVATION_REQUEST);



        //Also contains code pertaining to unrelated Google Cloud messaging features
        //and an onClick() method, all inside of onCreate()


        }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ACTIVATION_REQUEST:
            if (resultCode == Activity.RESULT_OK) {
                Log.i("DeviceAdminSample", "Administration enabled!");
            }
            else {
                Log.i("DeviceAdminSample", "Administration enable FAILED!");
            }
            return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

使用此代码,当我运行我的应用程序时, Activity 出现,然后动画看起来像是在打开一个新 Activity ,但随后动画停止并保留在当前 Activity 中,而不会提示用户启用设备管理.我的与设备管理员有关的日志消息都没有被命中。但是,在“设置”->“安全”->“设备管理员”页面上查看当前的设备管理员时,会显示我的应用程序,但未将其作为设备管理员选中。

这是我的 DeviceAdmin 子类的代码,但我在这段代码中看不出问题出在哪里:
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class DeviceAdmin extends DeviceAdminReceiver {

    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Log.i("Device Admin: ", "Enabled");
    }

    @Override
    public String onDisableRequested(Context context, Intent intent) {
        return "Admin disable requested";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Log.i("Device Admin: ", "Disabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.i("Device Admin: ", "Password changed");
    }

}

任何帮助将不胜感激!

更新:我的 Android list 中的设备管理接收器如下:
    <!-- Device Administration Receiver -->
    <receiver
        android:name="com.myPackage.DeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
                   android:resource="@xml/device_admin" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
        </intent-filter>
    </receiver>

最佳答案

我猜你忘了用正确的 Intent 过滤器在 list 中添加接收器

<receiver
        android:name="com.yourpackage.DeviceAdmin"
        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 xmlns:android="http://schemas.android.com/apk/res/android" >

<uses-policies>
    <limit-password />

    <watch-login />

    <reset-password />

    <force-lock />

    <wipe-data />

    <expire-password />

    <encrypted-storage />

    <disable-camera />
</uses-policies>

</device-admin>

有关设备管理的更多信息。 Check this

关于Android 设备管理员权限 Activity 未启动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27916849/

10-11 09:21