Как подтвердить, что услуга является приоритетной - PullRequest
0 голосов
/ 21 октября 2018

У меня есть служба воспроизведения музыки (медиаплеер), и она работает нормально.

  1. Как подтвердить, что эта служба является службой переднего плана?
  2. Как смоделировать системузагрузить и проверить, будет ли служба уничтожена или нет?

    Это мой код.

    public int onStartCommand (намеренное намерение, int flags, int startId) {initMediaSession ();initMediaPlayer ();buildNotification (PlaybackStatus.PLAYING);
    return super.onStartCommand (намерение, флаги, startId);}

    private void buildNotification (PlaybackStatus playStatus) {

    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 Ответ

0 голосов
/ 21 октября 2018
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 на переднем плане?

...