Android 2.2 SDK提供了一個可管理和操作設備的API叫DevicePolicyManager,使用這個API你可以接管手機的應用權限,對手機做出很多大膽的操作,比如鎖屏、恢復出廠設置、設置密碼、強制清除密碼,修改密碼、設置屏幕燈光漸暗時間間隔等操作。
這個API讓你的程序直接掌握了系統的命脈。
DeviceAdminReceiver的具體實現類DeviceAdmin ,它是繼承自Receiver 廣播接收者;
package com.example.lock;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
/**
* DeviceAdminReceiver的具體實現類它是繼承自Receiver 廣播接收者;
* @author Administrator
*
*/
public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
}
package com.example.lock;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class MainActivity extends Activity implements OnClickListener {
private Button bt_re_pw, bt_lock, bt_other, bt_clean;
private ComponentName mDeviceAdminSample;// 組件對象;
private DevicePolicyManager mDPM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
mDeviceAdminSample = new ComponentName(this, DeviceAdmin.class);// 廣播接收者對應的字節碼文件;
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
}
private void initUI() {
bt_clean = (Button) findViewById(R.id.bt_clean);
bt_re_pw = (Button) findViewById(R.id.bt_re_pw);
bt_lock = (Button) findViewById(R.id.bt_lock);
bt_other = (Button) findViewById(R.id.bt_other);
bt_clean.setOnClickListener(this);
bt_re_pw.setOnClickListener(this);
bt_lock.setOnClickListener(this);
bt_other.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_clean:// 在真機上請慎用;
if (mDPM.isAdminActive(mDeviceAdminSample)) {// 設備管理器是否激活
// mDPM.wipeData(0);//WIPE_EXTERNAL_STORAGE清楚sd卡的數據;
// mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//WIPE_EXTERNAL_STORAGE清楚sd卡的數據;
} else {
Toast.makeText(this, "請先激活", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_re_pw://卸載應用程序
Intent intent = new Intent("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + getPackageName()));//根據唯一的包名進行卸載;
startActivity(intent);
break;
case R.id.bt_lock:
if (mDPM.isAdminActive(mDeviceAdminSample)) {// 設備管理器是否激活
mDPM.lockNow();
// 鎖屏的同時激活密碼;
mDPM.resetPassword("123", 0);
} else {
Toast.makeText(this, "請先激活", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_other:
Intent intent2 = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent2.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mDeviceAdminSample);
intent2.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "設備管理器");
startActivity(intent2);
break;
default:
break;
}
}
}
在res目錄下創建一個文件夾命名爲xml在其中創建一個xml文件device_admin_sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.lock.DeviceAdmin"
android:description="@string/sample_device_admin_description"
android:label="@string/sample_device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android: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>
</application>
</manifest>
其中的命名就是隨便寫的請大神見諒。