【问题标题】:Foreground notification still appearing after Intent service destroyedIntent服务销毁后前台通知仍然出现
【发布时间】:2021-01-04 12:03:04
【问题描述】:

我有一个 IntentService,在这个即时服务中,在onCreate 中,我调用了startforeground() 方法。然后我会在创建 intentService 时看到通知。但是,当 IntentService 被销毁(转到 onDestroy)时,我可以在服务被销毁后的几秒钟内看到通知。这是为什么呢?

这是IntentService的代码:

public class USFIntentService extends IntentService {

    private static final String TAG = "USFIntentService";

    private static final int USF_NOTIFICATION_ID = 262276;
    private static final String USF_NOTIFICATION_CHANNEL_ID = "USF_NOTIFICATION_CHANNEL";

    public USFIntentService() {
        super("USFIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG,"in onCreate");
        startUsfForegroundService();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"in onDestroy");
    }

    private void startUsfForegroundService() {
        // Define notification channel
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel =
                new NotificationChannel(USF_NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

        // Build notification to be used for the foreground service.
        Notification notification =
                new Notification.Builder(this, USF_NOTIFICATION_CHANNEL_ID)
                        .setContentTitle(getText(R.string.notification_title))
                        .setContentText(getText(R.string.notification_message))
                        .setSmallIcon(R.drawable.usf_notification_icon)
                        .build();

        // Set the service as a foreground service.
        startForeground(USF_NOTIFICATION_ID, notification);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "onHandleIntent");
        if (intent != null) {
            doStuff();
        }
        Log.i(TAG,"End of onHandleIntent");
    }

}

我这样称呼这个服务:

Intent startServiceIntent = new Intent(intent);
startServiceIntent.setComponent(new ComponentName(context, USFIntentService.class));
context.startForegroundService(startServiceIntent); 

【问题讨论】:

    标签: android intentservice foregroundnotification


    【解决方案1】:

    完成工作后尝试致电Service#stopForeground 将其删除

    【讨论】:

    • 我实际上尝试在 onDestroy 中调用 stopForeground(true),但几秒钟后我仍然看到它
    【解决方案2】:

    完成这些工作后,您可以致电stopForeground(true) 。这样您的服务就会立即从前台状态中删除,并且参数true 确保通知将被删除。

    【讨论】:

    • 我实际上尝试在 onDestroy 中调用 stopForeground(true),但几秒钟后我仍然看到它
    • 在 onDestroy(IntentService 的)中
    • 尝试在onHandleIntent 中调用它。 onDestroy 可能会在延迟后调用
    • 是一样的(其实我看到onDestroy调用了但是我还是看到了通知)
    【解决方案3】:

    就我而言,它发生在 android R (Pixel3) 中。

    我添加了ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH) 并立即关闭通知。

    我不知道,为什么它有效..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多