代码之家  ›  专栏  ›  技术社区  ›  صلي علي محمد - Atef Farouk

如何确认服务是前台服务

  •  0
  • صلي علي محمد - Atef Farouk  · 技术社区  · 8 年前

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

    1. 如何确认这个服务是前台服务?
    2. 如何模拟系统负载并检查服务是否将被终止?

      这是我的密码。

      public int onStartCommand(Intent Intent,int flags,int startId){ 构建通知(PlaybackStatus.PLAYING);
      }

      私有void buildNotification(PlaybackStatus 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());
      

      }

    1 回复  |  直到 8 年前
        1
  •  -1
  •   Asad Ali    8 年前
    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;
    

    }

    如果您想知道您的服务是否在前台运行,只需打开其他一些fat应用程序,然后检查服务是否仍在运行,或者只需检查标志service.foreground

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