其主要功能可以使用管理者權限讓螢幕上鎖等等得功能
今天要實做的上鎖功能,必須實做的元件有Activity、DeviceAdminReceiver以及一個DeviceAdminInfo會使用的Xml檔案。
整體結構如下圖:
重點在於實做了DeviceAdminReceiver這個廣播,註冊時要將DeviceAdminInfo所使用的Xml一起註冊在DeviceAdminReceiver廣播內,必須幫廣播加入下方的權限
android:permission="android.permission.BIND_DEVICE_ADMIN"
Activity程式碼如下:
package com.example.demo;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
protected final int UnRegistration = 0;
protected final int LockScreen = 1;
private DevicePolicyManager devicePolicyManager;
private ComponentName componentName;
private Button btn_Lock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
protected void onPause() {
UnReg_Lock(UnRegistration);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
private void init() {
devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, AdminReceiver.class);
btn_Lock = (Button) findViewById(R.id.button1);
btn_Lock.setOnClickListener(lock);
Registration();
}
private void Registration() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"程式的描述");
startActivityForResult(intent, 0);
}
private void UnReg_Lock(int sltNum) {
if (devicePolicyManager.isAdminActive(componentName)) {
switch (sltNum) {
case UnRegistration:
devicePolicyManager.removeActiveAdmin(componentName);
break;
case LockScreen:
devicePolicyManager.lockNow();
break;
}
}
}
private Button.OnClickListener lock = new Button.OnClickListener() {
public void onClick(View arg0) {
UnReg_Lock(LockScreen);
}
};
}
DeviceAdminReceiver程式碼如下:
package com.example.demo;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AdminReceiver extends DeviceAdminReceiver {
private final String Tag = "Tag:";
public DevicePolicyManager getManger(Context context) {
Log.e(Tag, "調用getManger()");
return super.getManager(context);
}
@Override
public IBinder peekService(Context myContext, Intent service) {
Log.e(Tag, "調用peekService()");
return super.peekService(myContext, service);
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
Log.e(Tag, "調用onDisableRequested()");
return super.onDisableRequested(context, intent);
}
@Override
public void onDisabled(Context context, Intent intent) {
Log.e(Tag, "管理權限取消");
super.onDisabled(context, intent);
}
@Override
public void onEnabled(Context context, Intent intent) {
Log.e(Tag, "管理權限開啟");
super.onEnabled(context, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.e(Tag, "調用onReceive()");
super.onReceive(context, intent);
}
}
deviceinfo.xml:
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<!-- 今天只使用螢幕鎖定的功能,所以沒有加入其他資訊 -->
<force-lock />
</uses-policies>
</device-admin>
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="AdminReceiver"
android:description="@string/description"
android:label="@string/label"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/deviceinfo" />
</receiver>
</application>
</manifest>
參考文章:
http://developer.android.com/guide/topics/admin/device-admin.html
http://android-zhang.iteye.com/blog/1487077
http://developer.android.com/reference/android/app/admin/DeviceAdminInfo.html
http://developer.android.com/reference/android/app/admin/DeviceAdminReceiver.html
http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html