设备管理器有个特点,你注册了之后如果不解除注册就会难以卸载带有设备管理器的应用,目前4.3版本仍未提示用户如何卸载,maybe later.
在「设定-安全」你可以看见「设备管理器」,它提供一些高级功能,如下:
<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>
这个XML文件由你自己定义,这些权限写在这个文件里。你可以选择其中的权限。
下面这个例子是,在打开的时候判断有没有注册设备管理器,没有注册则提示注册,否则Toast「已经注册」。
1.MainActivity.java
package com.example.wannauninstall; import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast; public class MainActivity extends Activity { DevicePolicyManager devicePolicyMNG ;
ComponentName componentName ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
devicePolicyMNG = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this,DeAdReceiver.class); if(devicePolicyMNG.isAdminActive(componentName))
{
Toast.makeText(MainActivity.this, "已经注册", Toast.LENGTH_LONG).show();
}
else ActiveMNG(); }
private void ActiveMNG()
{
Intent intent = new Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"--其他描述--");
startActivityForResult(intent , 0 ); } }
and you gonna need this:
2.DeAdReceiver.java
package com.example.wannauninstall; import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; public class DeAdReceiver extends DeviceAdminReceiver{ @Override
public void onEnabled(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("TAG000","------onEnabled-------"); super.onEnabled(context, intent);
} @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("TAG000","--------onReceive-----"); super.onReceive(context, intent);
} }
每次打开应用,你都会在LOGCAT里发现TAG000的onReceive的LOG,而如果注册了还可以看到onEnabled的LOG。
3.DeviceAdminReceiver是继承BroadcastReceiver的,所以也需要在Manifest中注册。类似这样:
<receiver
android:name="com.example.wannauninstall.DeAdReceiver"
android:label="System 设备管理器"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@layout/lock_screen" /> <intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
注意上面的intent-filter里面的过滤条件,和前面第32行的implicit intent对应。
and you gonna see this:
It's a wit from:http://blog.csdn.net/feng88724/article/details/6323544,check it for more.
mar.20 Larry