最近要完成这样一个功能的DEMO:自己写一个锁屏界面,要做到当手机启动开机或锁屏时先调用自己的锁屏界面解锁,解锁成功后再调用
系统的解锁界面进行解锁(相当于给手机加两道锁)研究了好久,网上好多都是说android KeyguardManager.KeyguardLock kk的用法、
但没有一个完整代码的DEMO来说明,其实就是对KK的控制逻辑方面稍微复杂一点,一下就是我完成此功能的详细代码和解释,希望对
ANDROID工作者有帮助:上代码:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.archermind"
android:versionCode="1"
android:versionName="1.0" >
<!-- need by application -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- need by lockScreen -->
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".LockScreenActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".UnLockScreenActivity" >
</activity>
<activity
android:label="@string/app_name"
android:name=".LockView" >
</activity>
<service
android:name=".MyService" />
<receiver
android:name=".MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_policies" />
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
加锁界面:LockScreenActivity.java
package com.archermind;
import java.lang.reflect.Method;
import android.content.Intent;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LockScreenActivity extends Activity {
private Button action;
private EditText password;
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mcomponentName ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mcomponentName = new ComponentName(this, MyAdiminReceiver.class);
password=(EditText) findViewById(R.id.password);
action=(Button) findViewById(R.id.action);
action.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("systemlock", "-----systemlock---");
SharedPreferences sp=getSharedPreferences("lockscreen",Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("password", password.getText().toString());
editor.commit();
Intent lockintent = new Intent();
lockintent.setClass(LockScreenActivity.this, MyService.class);
startService(lockintent);
Lock() ;
LockScreenActivity.this.finish();
}
});
}
private void Lock() {
// TODO Auto-generated method stub
Log.e("lockscreen","------ Lock Screen ------");
boolean active = mDevicePolicyManager.isAdminActive(mcomponentName);
if (!active) {
// Intent intent = new Intent(
// DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
// mcomponentName);
// startActivity(intent);
setActiveAdmin(mDevicePolicyManager,mcomponentName);
mDevicePolicyManager.lockNow();
}else{
mDevicePolicyManager.lockNow();
}
}
private void setActiveAdmin(DevicePolicyManager devicePolicyManager,ComponentName componentName) {
Log.e("setActiveAdmin","------ setActiveAdmin ------");
boolean isMediaPad = false;
Class<?>[] clsArray = isMediaPad ? new Class[]{ComponentName.class, boolean.class} : new Class[]{ComponentName.class};
Object[] objArray = isMediaPad ? new Object[]{componentName, false} : new Object[]{componentName};
Class <DevicePolicyManager> c = DevicePolicyManager.class;
Method setActiveAdmin = null;
try {
setActiveAdmin = c.getDeclaredMethod("setActiveAdmin", clsArray );
setActiveAdmin.setAccessible(true);
setActiveAdmin.invoke(devicePolicyManager, objArray);
} catch (Exception e) {
Log.e("LockScreenActivity", "Fail at:setActiveAdmin unsuccessfuly!\n" + e.toString() + "\n");
}
}
//
//
//
//}
}
服务代码 MyService.java
package com.archermind;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service{
private Intent startIntent=null;
public static KeyguardManager.KeyguardLock kk;
private static final String DEBUG_TAG = "======Service=====";
// private void registerIntentReceivers(){
// Log.d(DEBUG_TAG, "***registerIntentReceivers");
// IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
// filter.addAction("android.intent.action.SCREEN_ON");
// registerReceiver(receiver, filter);
//
// }
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
startIntent=intent;
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction("android.intent.action.SCREEN_ON");
registerReceiver(receiver, filter);
System.out.println("service onStart and action is "+intent.getAction());
Log.e("service", "-----serviceonstart---");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("service onDestroy");
unregisterReceiver(receiver);
if(startIntent!=null){
System.out.println("serviceIntent not null");
startService(startIntent);
}
}
private BroadcastReceiver receiver = new
android 自定义锁屏凌驾于系统锁屏之上
需积分: 50 154 浏览量
2019-04-04
01:14:45
上传
评论 2
收藏 5KB RAR 举报
weixin_38669628
- 粉丝: 383
- 资源: 6万+