一键清除SD卡数据

来源:互联网 发布:梅甘娜rs 知乎 编辑:程序博客网 时间:2024/06/05 16:10

1、Activity中锁屏的代码实现

import android.app.Activity;import android.app.admin.DevicePolicyManager;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {private DevicePolicyManager dpm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//setContentView(R.layout.activity_main);dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);wipedata(null);}/** * @param v * 一键清除SD卡数据 */public void wipedata(View v){//如果没有激活设备管理员,提醒给用户做事ComponentName who = new ComponentName(this, DeviceAdminSample.class);if (dpm.isAdminActive(who)) {//true//清除sd数据dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);finish();} else {  Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);              intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,                     "设备管理器,,,,,,,,,,,,,,,,");              startActivityForResult(intent, 1);}// 取消激活设备管理//DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);//ComponentName who = new ComponentName(this, DeviceAdminSample.class);//dpm.removeActiveAdmin(who);//取消激活管理设备}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
2、设备管理员接收者类的实现

import android.app.admin.DeviceAdminReceiver;public class DeviceAdminSample extends DeviceAdminReceiver {}
3、在清单文件中声明设备管理员接收者
<receiverandroid:name="com.lqr.lockscreen.DeviceAdminSample"android:description="@string/sample_device_admin_description"android:label="@string/sample_device_admin"android:permission="android.permission.BIND_DEVICE_ADMIN" ><meta-dataandroid:name="android.app.device_admin"android:resource="@xml/device_admin_sample" /><intent-filter><action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /></intent-filter></receiver>
4、在res目录下新建xml文件夹,创建一个device_admin_sample.xml文件,内容如下:
<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>
5、string.xml内容如下:
<resources>    <string name="app_name">一键清除SD卡数据</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="activity_sample_device_admin">设备管理员</string>    <string name="sample_device_admin">管理员</string>    <string name="sample_device_admin_description">开启设备管理员,这是设备器中的描述</string></resources>


0 0
原创粉丝点击