java - 如何确认服务是前台服务
问题描述
我有一个播放音乐的服务(媒体播放器),它工作正常。
- 如何确认此服务是前台服务?
如何模拟系统负载并检查服务是否会被杀死?
这是我的代码。
public int onStartCommand(Intent intent, int flags, int startId) { initMediaSession(); 初始化媒体播放器();buildNotification(PlaybackStatus.PLAYING);
return super.onStartCommand(intent, flags, startId); }私人无效构建通知(播放状态播放状态){
final int NOTIFY_ID = 1002; String aMessage=""; String name = "my_package_channel"; String id = "my_package_channel_1"; // The user-visible name of the channel. String description = "my_package_first_channel"; // The user-visible description of the channel. Intent intent; PendingIntent pendingIntent; NotificationCompat.Builder builder; if (notifManager == null) { notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = notifManager.getNotificationChannel(id); if (mChannel == null) { mChannel = new NotificationChannel(id, name, importance); mChannel.setDescription(description); notifManager.createNotificationChannel(mChannel); } builder = new NotificationCompat.Builder(this, id); intent = new Intent(this, NotificationReturnSlot.class); pendingIntent = PendingIntent.getActivity(this, 1, intent, 0); builder.setContentTitle(aMessage) // required .setSmallIcon(R.mipmap.ic_launcher) .setContentText(this.getString(R.string.app_name)) // required .setAutoCancel(false) .setContentIntent(pendingIntent) .setVibrate(new long[]{0L}) ; } else { builder = new NotificationCompat.Builder(this); intent = new Intent(this, NotificationReturnSlot.class); pendingIntent = PendingIntent.getActivity(this, 1, intent, 0); builder.setContentTitle(aMessage) // required .setSmallIcon(R.mipmap.ic_launcher) .setContentText(this.getString(R.string.app_name)) // required .setAutoCancel(false) .setContentIntent(pendingIntent) .setVibrate(new long[]{0L}) .setPriority(Notification.PRIORITY_HIGH); } int mId = 1489; startForeground(mId, builder.build());
}
解决方案
private boolean isServiceRunning(String serviceName){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
if(runningServiceInfo.foreground)
{
//service run in foreground
}
}
}
return serviceRunning;
}
如果您想知道您的服务是否在前台运行,只需打开其他一些胖应用程序,然后检查服务是否仍在运行或检查标志 service.foreground
推荐阅读
- node.js - fs.readFileSync 与 s3.getObject 的等价物是什么
- google-admin-sdk - Google Meet 审核日志中的最大丢包率指标是什么意思?
- python - 如何在 jupyter notebook 的下图中修复我的情节?
- javascript - 每次我尝试在我的网站上发布内容时都会出现 504 错误
- express - 使用与应用程序不同的端口时,Express 会话不会在浏览器上设置 cookie
- reactjs - 独立于后端对反应前端进行 docker 化是一种好习惯吗?
- c++ - 不使用循环查找部分总和
- r - 在 r 中使用 for 循环和/或函数优化和改进代码
- clang - 从 ClassTemplateSpecializationDecl 中获取专门的方法体?
- javascript - 请求是从客户端到后端还是从前端到后端?