Android 4.0 Device Admin Receiver无法正常工作 [英] Android 4.0 Device Admin Receiver not working

查看:98
本文介绍了Android 4.0 Device Admin Receiver无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个非常简单的应用程序,以清除ICS设备上的所有用户数据.

I'm trying to build a really simple app to wipe all the user data off a ICS device.

我尝试使用 http ://developer.android.com/guide/topics/admin/device-admin.html http://marakana.com/s/post/1291/android_device_policy_administration_tutorial

但是我遇到了一个问题,无论我做什么,都没有出现提示用户允许管理员的广播接收器!

But I'm having an issue, no matter what I do, the broadcast receiver for prompting the user to allow admin does not appear!

如果有人可以帮助解决此问题,我在这里到目前为止的结果.

Heres what I got so far if anyone can help with this issue.

清单:

Manifest:

<uses-sdk android:minSdkVersion="15" />

<application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity     android:name="com.CheckActivityService.CheckActivityServiceActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"     />
        </intent-filter>
    </activity>
</application>


<receiver android:name=".mDeviceAdminReceiver"
    android:label="device_admin"     android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
        android:resource="@xml/policies" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>


</manifest>

活动:

public class CheckActivityServiceActivity extends Activity implements                          OnCheckedChangeListener{

 /** Called when the activity is first created. */

static final int ACTIVATION_REQUEST = 1; // identifies our request id
DevicePolicyManager devicePolicyManager;
ComponentName deviceAdmin;
ToggleButton toggleButton;
static final String TAG = "DevicePolicyActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    toggleButton = (ToggleButton) super
        .findViewById(R.id.toggle_device_admin);
    toggleButton.setOnCheckedChangeListener(this);

    Button btn = (Button) findViewById(R.id.wipeDataBtn);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            wipeData();
        }
    });

    devicePolicyManager = (DevicePolicyManager)     getSystemService(Context.DEVICE_POLICY_SERVICE);
    deviceAdmin = new ComponentName(CheckActivityServiceActivity.this,      mDeviceAdminReceiver.class);
     }

/**
 * Called when the state of toggle button changes. In this case, we send an
 * intent to activate the device policy administration.
 */
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        if (isChecked) {
            // Launch the activity to have the user enable our admin.
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "My Boss told me     to do this!!");
            startActivityForResult(intent, ACTIVATION_REQUEST);
        }
        Log.d(TAG, "onCheckedChanged to: " + isChecked);
}

public void wipeData(){
    devicePolicyManager.wipeData(ACTIVATION_REQUEST);
}

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

收件人:

 public class mDeviceAdminReceiver extends DeviceAdminReceiver {

    static final String TAG = "DeviceAdminReceiver";

    void showToast(Context context, String msg) {
        String status = "TEST";
        Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
    }

    /** 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, "Admin Enabeled",
                            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, "Admin 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");
    }
}

如果有人可以帮助我使其正常工作,因为我真的不知道为什么广播接收器不触发.

If anyone can help me to get this to work, as I really can't figure out why the broadcast receiver isn't firing.

推荐答案

想出了问题,对我来说是愚蠢的.

Figured out problem, kind of stupid of me.

需要将接收器放入元素中.

needed to put the receiver in the element.

这篇关于Android 4.0 Device Admin Receiver无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆