设备管理器有个特点,你注册了之后如果不解除注册就会难以卸载带有设备管理器的应用,目前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
1 package com.example.wannauninstall; 2 3 import android.app.Activity; 4 import android.app.admin.DevicePolicyManager; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.Menu; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 14 DevicePolicyManager devicePolicyMNG ; 15 ComponentName componentName ; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 devicePolicyMNG = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 21 componentName = new ComponentName(this,DeAdReceiver.class); 22 23 if(devicePolicyMNG.isAdminActive(componentName)) 24 { 25 Toast.makeText(MainActivity.this, "已经注册", Toast.LENGTH_LONG).show(); 26 } 27 else ActiveMNG(); 28 29 } 30 private void ActiveMNG() 31 { 32 Intent intent = new Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 33 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); 34 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"--其他描述--"); 35 startActivityForResult(intent , 0 ); 36 37 } 46 }
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