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

想做个手机卫士的功能,用到锁屏,所有上网 查了下,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>
如果你觉得按电源键锁屏麻烦或对电源键有伤害,你点击图标就可以直接点击图标锁屏,是不是很方便。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值