接受系统开机广播的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 {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
context.startService(new Intent(context, LockScreenService.class));
}
}
}
锁屏显示的界面
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.activity_main).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.startService(new Intent(MainActivity.this, LockScreenService.class));
}
});
}
}
<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>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android: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>
处理锁屏的Service
处理锁屏的Service
package 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() {
@Override
public 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
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
kk = km.newKeyguardLock("");
kk.disableKeyguard();
}
@Override
public 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);
}
@Override
public void onDestroy() {
kk.reenableKeyguard();
super.onDestroy();
}
}