gpt4 book ai didi

java - 应用关闭时通知仍然出现

转载 作者:行者123 更新时间:2023-11-29 00:03:11 24 4
gpt4 key购买 nike

我试图在我的应用程序关闭时关闭通知,因为当我从(最近的任务/滑动退出)关闭应用程序时,通知仍然出现,如果用户尝试点击通知,应用程序将崩溃。

 @Override
protected void onDestroy() {
super.onDestroy();
notification.cancel(1);
try {
MApplication.sBus.unregister(this);
} catch (Exception e) {
e.printStackTrace();
}
}

onDestroy() 中但不起作用,因为 onDestroy() 不是每次都被调用。

最佳答案

问题解决了

添加一个java类文件。这里的文件名是KillNotificationService.java

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class KillNotificationService extends Service{

@Override
public void onTaskRemoved(Intent rootIntent) {
//Toast.makeText(this, “service called: “, Toast.LENGTH_LONG).show();
super.onTaskRemoved(rootIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getSystemService(ns);
nMgr.cancelAll();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

在您的 MainActivity oncreate 方法中调用此服务。

startService(new Intent(this, KillNotificationService.class)); 

Manifest文件中添加服务

<service android:name=".KillNotificationService"/>

关于java - 应用关闭时通知仍然出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48346933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com