安卓设备管理员权限活动未启动

4

我正在尝试将我的应用程序启用为设备管理员。尝试调用包含在我的应用程序的第一个活动中的允许我的应用程序成为设备管理员的活动的代码如下:

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);
}

使用此代码时,当我运行应用程序时,活动出现,然后进行动画,看起来像是打开了一个新的活动,但然后该动画停止,并保留在当前活动上,而不提示用户启用设备管理员。与设备管理员相关的我的所有日志消息都没有被触发。然而,当查看设置->安全->设备管理员页面上的当前设备管理员时,我的应用程序会显示出来,但未选中为设备管理员。
这是我的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清单中的设备管理员接收器如下:
    <!-- 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>        
3个回答

6
我猜您在清单文件中忘记添加正确的意图过滤器来指定接收者。
<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>

更多有关设备管理的信息,请查看这个链接


3
我在Jagadesh的帮助下找到了问题所在。
在我的Android清单文件中,我将接收器名称设置为DeviceAdminReceiver,如下所示:
<receiver
    android:name="com.myPackage.DeviceAdminReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN">

我犯了一个错误,接收器应该使用类名DeviceAdminReceiver而不是我编写的子类DeviceAdmin,该子类扩展了DeviceAdminReceiver

在清单文件中更新后,工作正常的接收器如下:

    <!-- Device Administration Receiver -->
    <receiver
        android:name="com.myPackage.DeviceAdmin"
        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>        

0

我曾经遇到过同样的问题,通过在清单文件中删除以下内容进行了修正:

android:launchMode="singleInstance"

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