如何确认服务是前台服务

Posted

技术标签:

【中文标题】如何确认服务是前台服务【英文标题】:How to confirm service is a foreground service 【发布时间】:2019-03-25 13:21:36 【问题描述】:

我有一个播放音乐的服务(媒体播放器),它工作正常。

    如何确认此服务是前台服务?

    如何模拟系统负载并检查服务是否会被杀死?

    这是我的代码。

    public int onStartCommand(Intent intent, int flags, int startId) 初始化媒体会话(); 初始化媒体播放器(); buildNotification(PlaybackStatus.PLAYING); return super.onStartCommand(intent, flags, startId);

    private void buildNotification(PlaybackStatus 播放状态)

    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());
    

【问题讨论】:

***.com/questions/6452466/… 的副本 How to check if a service is running on android?的可能重复 【参考方案1】:
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

来源:How to determine if an Android Service is running in the foreground?

【讨论】:

以上是关于如何确认服务是前台服务的主要内容,如果未能解决你的问题,请参考以下文章

如何在科尔多瓦运行android前台服务?

如何从前台服务启动媒体投影

打盹模式如何影响后台/前台服务,有/没有部分/全部唤醒锁?

如何每 5 分钟运行 10 秒的前台服务?

如何像前台服务一样启动 socket.io 以保持连接并监听消息

android前台服务以及通知