实现思路
- 该锁屏的实现思路是在锁屏显示的时候,屏蔽掉系统原来的屏保,显示自己的屏保程序
- 锁屏调用的时机有两个,一个是在手机开机时(可以监听开机广播),第二个是在屏幕灭屏/亮屏切换时候(可以监听屏幕灭/亮的广播)。
实现
SystemEventReceiver
接受系统开机广播的Receiver,并启动Service
package com.lockscreen;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;/*** Created by jun on 17-4-24.*/
public class SystemEventReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {context.startService(new Intent(context, LockScreenService.class));}}
}
MainActivity
锁屏显示的界面
package com.lockscreen;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.activity_main).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {MainActivity.this.startService(new Intent(MainActivity.this, LockScreenService.class));}});}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.lockscreen"><!--解除键盘锁 所需要权限--><uses-permission android:name="android.permission.DISABLE_KEYGUARD" /><!--开机广播--><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:excludeFromRecents="true"><!--使应用不再最近应用列表中显示--><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".LockScreenService"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></service><!--处理系统广播--><receiver android:name=".SystemEventReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver></application>
</manifest>
LockScreenService
处理锁屏的Service
处理锁屏的Servicepackage com.lockscreen;import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;/*** Created by jun on 17-4-24.*/public class LockScreenService extends Service {private final String TAG = this.getClass().getName();private KeyguardManager km;private KeyguardManager.KeyguardLock kk;private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "onReceive: start");KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);KeyguardManager.KeyguardLock kk = km.newKeyguardLock("");kk.disableKeyguard();Intent service = new Intent();service.setClass(context, MainActivity.class);service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(service);}};@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);kk = km.newKeyguardLock("");kk.disableKeyguard();}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Log.d(TAG, "onStart: ");//通知状态条notifySpinnerBar();//亮屏/灭屏广播只能动态监听IntentFilter iFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);iFilter.setPriority(1000);this.registerReceiver(broadcastReceiver, iFilter);}//将本服务显示到SpinnerBar上private void notifySpinnerBar() {Notification notify = new Notification(R.mipmap.ic_launcher, null, 0);//将此通知放到通知栏的Ongoing组中,也就是正在运行的组中notify.flags |= Notification.FLAG_ONGOING_EVENT;
// notify.flags |= Notification.FLAG_AUTO_CANCEL;
// notify.flags |= Notification.FLAG_FOREGROUND_SERVICE;
// notify.flags |= Notification.FLAG_INSISTENT;
// notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
// notify.flags |= Notification.FLAG_SHOW_LIGHTS;
// notify.flags |= Notification.FLAG_NO_CLEAR;//定义Notification出现声音notify.defaults |= Notification.DEFAULT_SOUND;//设置如何震动notify.defaults |= Notification.DEFAULT_LIGHTS;//设置lec灯颜色notify.ledARGB = Color.BLUE;notify.ledOnMS = 5000;Intent notifyIntent = new Intent(this, MainActivity.class);PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
// notify.setLatestEventInfo(this,null,null,contentIntent);NotificationManager notifyManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);this.startForeground(0, notify);//当id=0时,notify将不会显示
// this.startForeground(0, notify);}@Overridepublic void onDestroy() {kk.reenableKeyguard();super.onDestroy();}
}