簡體   English   中英

如何確認服務是前台服務

[英]How to confirm service is a foreground service

我有一個播放音樂的服務(媒體播放器),並且工作正常。

  1. 如何確認該服務是前台服務?
  2. 如何模擬系統負載並檢查服務是否將被終止?

    這是我的代碼。

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

    私人無效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;

}

如果您想知道您的服務是否正在前台運行,請打開其他胖應用程序,然后檢查服務是否仍在運行或僅檢查標志service.foreground

資料來源: 如何確定Android服務是否在前台運行?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM