一键锁屏

源码下载地址:

http://download.csdn.net/detail/flying_vip_521/4449409


关键代码:

package com.innofidei.lock;

import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class LockScreen extends DeviceAdminReceiver {
	static final int RESULT_ENABLE = 1;

	public static class Controller extends Activity {

		DevicePolicyManager mDPM;
		ComponentName mDeviceAdminSample;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			// 首先我们要获得android设备管理代理
			mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

			// LockScreen 继承自 DeviceAdminReceiver
			mDeviceAdminSample = new ComponentName(Controller.this, LockScreen.class);
			// 得到当前设备管理器有没有激活
			boolean active = mDPM.isAdminActive(mDeviceAdminSample);
			if (!active) {
				// 如果没有激活的话,就去提示用户激活(第一次运行程序时)
				getAdmin();
			} else {
				// 如果已经激活的话,就执行立即锁屏
				mDPM.lockNow();
			}

			// killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费;
			android.os.Process.killProcess(android.os.Process.myPid());
		}

		public void getAdmin() {
			// Launch the activity to have the user enable our admin.
			Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
			intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
			intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "One key lock screen need to active");
			startActivityForResult(intent, RESULT_ENABLE);
		}
	}

}



AndroidManifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.innofidei.lock"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application android:label="@string/app_name" >
        <activity
            android:name=".LockScreen$Controller"
            android:theme="@android:style/Theme.Translucent" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".LockScreen"
            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>

device_admin_sample.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>



你可能感兴趣的:(android,service,application,Class,action,encoding)