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;
@Override
protected 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);//取消激活管理设备
}
@Override
public 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、在清单文件中声明设备管理员接收者
4、在res目录下新建xml文件夹,创建一个device_admin_sample.xml文件,内容如下:
5、string.xml内容如下:
一键清除SD卡数据
Settings
Hello world!
设备管理员
管理员
开启设备管理员,这是设备器中的描述