您的位置:首页 > 移动开发 > Android开发

获得设备管理权限实现锁屏锁屏

2018-04-11 10:37 218 查看
想做个手机卫士的功能,用到锁屏,所有上网 查了下,google的官方直接给出了详细的文档。

1.文件清单

<span style="white-space:pre"> </span><!-- 设备管理 -->
<receiver
android:name="com.example.lockscreen.MyAdmin"
android:label="@string/app_name"
android:description="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action
android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>

2.设备管理类

<span style="white-space:pre"> public class MyAdmin extends DeviceAdminReceiver {
<span style="white-space:pre"> </span>
}</span>
就是这样,可以什么都不写

3.功能代码

 
<span style="font-size:14px;">public class LockScreen extends Activity {
//设备策略服务
private DevicePolicyManager dpm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
dpm=(DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

}
public void lockscreen(View view){
ComponentName componentName= new ComponentName(this,MyAdmin.class);
if (dpm.isAdminActive(componentName)) {
dpm.lockNow();
dpm.resetPassword("", 0);//空为 无密码
//清除SD卡数据
//	dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
//恢复出厂
//dpm.wipeData(0);
}else {
Toast.makeText(this, "请先点击激活安全权限按钮", 1).show();
}

}
//用代码开启管理员权限
public void openAdmin(View view){
// 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//我yao激活谁
ComponentName componentName= new ComponentName(this,MyAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "您必须开启此权限才可以使用一键锁屏");
startActivity(intent);

}
//卸载
public void uninstall(View view){
ComponentName componentName= new ComponentName(this,MyAdmin.class);
dpm.removeActiveAdmin(componentName);
//卸载应用
//    	Intent intent = new Intent();
//    	intent.setAction("");
Uri packageUri = Uri.parse("package:"+LockScreen.this.getPackageName());
Intent intent = new Intent(Intent.ACTION_DELETE,packageUri);
startActivity(intent);
}
}</span>

      4.布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lockscreen.MainActivity" >

<Button
android:onClick="lockscreen"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一键锁屏" />
<Button
android:onClick="openAdmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启安全管理员的权限" />
<Button
android:onClick="uninstall"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一键卸载" />
</RelativeLayout>

如果你觉得按电源键锁屏麻烦或对电源键有伤害,你点击图标就可以直接点击图标锁屏,是不是很方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息