1.PhoneReceiver类
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.tsq.junbanpt.utils.MessageEvent;
import org.greenrobot.eventbus.EventBus;
/**
* @author xuwei
* 监听手机锁屏 APP在前后台
*/
public class PhoneReceiver extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
final String SYSTEM_DIALOG_REASON_LOCK_KEY = "lock";
final String SYSTEM_DIALOG_REASON_RECENTAPPS_KEY = "recentapps";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//按HOME键
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null && (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY) || reason.equals(SYSTEM_DIALOG_REASON_LOCK_KEY) || reason.equals(SYSTEM_DIALOG_REASON_RECENTAPPS_KEY))) {
//app 按HOME键
EventBus.getDefault().post(new MessageEvent("监听APP在前后台", "HOME键"));
}
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
//app 锁屏
EventBus.getDefault().post(new MessageEvent("监听APP在前后台", "锁屏"));
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
//app 亮屏
EventBus.getDefault().post(new MessageEvent("监听APP在前后台", "亮屏"));
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
//app 解锁
EventBus.getDefault().post(new MessageEvent("监听APP在前后台", "解锁"));
}
}
}
2.前台NotificationService
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import com.tsq.junbanpt.R;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class NotificationService extends Service {
private static final String TAG = "NotificationService";
private NotificationManager notificationManager;
//通知的唯一标识号。
private int NOTIFICATION = R.string.notification_live_start;
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
// 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("notification_id");
}
// 额外添加:
// 【适配Android8.0】给NotificationManager对象设置NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
// 启动前台服务通知
startForeground(1, builder.build());
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDestroy() {
super.onDestroy();
notificationManager.cancel(NOTIFICATION);
}
}
3.在AndroidManifest中注册
4.应用到activity中
//初始化forgroundService
Intent forgroundService;
forgroundService = new Intent(this, NotificationService.class);
//广播注册
receiver = new PhoneReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.CLOSE_SYSTEM_DIALOGS");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.USER_PRESENT");
registerReceiver(receiver, intentFilter);
//销毁广播
@Override
protected void onDestroy() {
super.onDestroy();
//广播销毁
unregisterReceiver(receiver);
}
//处理退到后台之后又点击返回APP时 停止service
@Override
protected void onResume() {
super.onResume();
if (isAppForeground("com.tsq.junbanpt")) {
NetPhoneActivity.this.stopService(forgroundService);
}
}
/**
* 接受广播发送的消息
*
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(MessageEvent event) {
if (event.getType().equals("监听APP在前后台")) {
if (event.getContect().equals("HOME键")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NetPhoneActivity.this.startForegroundService(forgroundService);
} else {
NetPhoneActivity.this.startService(forgroundService);
}
} else if (event.getContect().equals("锁屏")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NetPhoneActivity.this.startForegroundService(forgroundService);
} else {
NetPhoneActivity.this.startService(forgroundService);
}
} else if (event.getContect().equals("亮屏")) {
if (isAppForeground("com.tsq.junbanpt")) {
NetPhoneActivity.this.stopService(forgroundService);
}
} else if (event.getContect().equals("解锁")) {
if (isAppForeground("com.tsq.junbanpt")) {
NetPhoneActivity.this.stopService(forgroundService);
}
}
}
}
/**
* 判断APP是否在前台
*/
private boolean isAppForeground(String packageName) {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(packageName)) {
return true;
}
return false;
}
5.这里没有对EventBus的使用做介绍,要是不会的话查一下,很简单