从前台服务通知中删除按钮

时间:2019-06-06 07:43:25

标签: android android-service

我曾经使用过前台服务,但是当应用处于bg模式时,它显示了我不想执行的已完成任务。如何删除?如果此行(.addAction(R.drawable.ic_cancel,getString(R.string.remove_location_updates),                         servicePendingIntent))已删除,bg服务不起作用。如果使用以下代码:“。setContentIntent(servicePendingIntent)”,则当我单击noti时,该应用程序不会打开,noti已关闭并且服务停止。我该如何解决?预先感谢

enter image description here

private Notification getNotification() {
    Intent intent = new Intent(this, LocationUpdatesService.class);

    CharSequence text = Utils.getLocationText(mLocation);

    // Extra to help us figure out if we arrived in onStartCommand via the notification or not.
    intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);

    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);


    PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LiveTrack.class), 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates),
                    servicePendingIntent)
            .setContentIntent(activityPendingIntent)
//                .setContentIntent(servicePendingIntent)
            .setContentText("App name")
            .setContentTitle(Utils.getLocationTitle(this))
            .setOngoing(true)
            .setPriority(Notification.PRIORITY_HIGH)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker(text)
            .setWhen(System.currentTimeMillis());

    // Set the Channel ID for Android O.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(CHANNEL_ID); // Channel ID
    }
    return builder.build();
}


private void onNewLocation(Location location) {

    mLocation = location;

    // Notify anyone listening for broadcasts about the new location.
    Intent intent = new Intent(ACTION_BROADCAST);
    intent.putExtra(EXTRA_LOCATION, location);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

    // Update notification content if running as a foreground service.
    if (serviceIsRunningInForeground(this)) {
        mNotificationManager.notify(NOTIFICATION_ID, getNotification());
    }
}

public boolean serviceIsRunningInForeground(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(
            Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
            Integer.MAX_VALUE)) {
        if (getClass().getName().equals(service.service.getClassName())) {
            if (service.foreground) {
                return true;
            }
        }
    }
    return false;
}

1 个答案:

答案 0 :(得分:0)

如果您要看official document,它会说

  

对于需要立即运行并且必须执行的用户启动的工作   要完成此操作,请使用前台服务。使用前台服务   告诉系统该应用正在执行重要的操作,   不应该被杀死。 用户可以通过   通知栏中显示不可撤销的通知。

即使在services文档中也说

  

前台服务执行一些操作,这些操作对于   用户。例如,音频应用将使用前台服务来播放   音轨。 前景服务必须显示通知。   即使没有用户,前景服务也将继续运行   与该应用进行交互。

因此,似乎在使用前台服务时必须有不可撤销的通知。