简体   繁体   English

如何确认服务是前台服务

[英]How to confirm service is a foreground service

I have a service playing music (media player) and it's working fine. 我有一个播放音乐的服务(媒体播放器),并且工作正常。

  1. How to coinfirm this service is a foreground service? 如何确认该服务是前台服务?
  2. How to simulate system load and check if the service will be killed or not? 如何模拟系统负载并检查服务是否将被终止?

    This is my code. 这是我的代码。

    public int onStartCommand(Intent intent, int flags, int startId) { initMediaSession(); public int onStartCommand(Intent intent,int flags,int startId){initMediaSession(); initMediaPlayer(); initMediaPlayer(); buildNotification(PlaybackStatus.PLAYING); buildNotification(PlaybackStatus.PLAYING);
    return super.onStartCommand(intent, flags, startId); 返回super.onStartCommand(intent,flags,startId); } }

    private void buildNotification(PlaybackStatus playbackStatus) { 私人无效buildNotification(PlaybackStatusplayingStatus){

     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;

} }

If you want to know if your service is running in foreground just open some others fat applications and then check if service is still running or just check flag service.foreground 如果您想知道您的服务是否正在前台运行,请打开其他胖应用程序,然后检查服务是否仍在运行或仅检查标志service.foreground

Source: How to determine if an Android Service is running in the foreground? 资料来源: 如何确定Android服务是否在前台运行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM