android 应用程序强制关闭后显示通知 [英] Notification shows after android app is force closed

查看:77
本文介绍了android 应用程序强制关闭后显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天不停地用谷歌搜索这个,但找不到任何东西.我的场景如下:

I have googled this non stop today and could not find anything. My scenario is the following:

我有一个 Android 应用,可以自动回复收到的消息.我有下面的代码来创建一个持久的(不可滑动的)通知,然后当应用程序通过 onDestroy 销毁时,通知被删除.但是,当我打开最近面板并滑动我的应用程序时,应用程序停止自动回复服务并且广播接收器停止,但是没有调用 onDestroy 并且通知仍然可见.

I have an Android app that auto replies to incoming messages. I have the below code to create a persistent (non swipe-able) notification and then when the app is destroyed via onDestroy the notification is removed. However, when I open the recents panel and swipe my app away, the app stops the auto reply service and the broadcast receiver stops, however onDestroy is not called and the notification is still visible.

public void notifCreate() {
    Intent i = new Intent(Main.this, Main.class);
    PendingIntent pi = PendingIntent.getActivity(Main.this, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder notifBuilder = new Notification.Builder(getApplicationContext());

    notifBuilder.setContentTitle(getString(R.string.app_name));
    notifBuilder.setContentText(getString(R.string.notification));
    notifBuilder.setContentIntent(pi);
    notifBuilder.setSmallIcon(R.drawable.ic_launcher);
    notifBuilder.setOngoing(true);
    Notification notif = notifBuilder.getNotification();
    notifManager.notify(1, notif);
}

public void notifDestroy() {
    NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.cancelAll();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
    loadPrefs();
}

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

@Override
protected void onPause() {
    super.onPause();

    notifCreate();
}

@Override
protected void onResume() {
    super.onResume();

    loadPrefs();
    checkStates();
    notifDestroy();
}

@Override
public void onDestroy() {
    super.onDestroy();

    notifDestroy();
    service = false;
}

我的目标如下:

如果应用程序被强制关闭等,我很想简单地销毁通知,或者如果可能的话,我不介意应用程序是一项服务,因此即使从最近刷卡,它仍然会运行.然而,最好的情况是,当我的广播接收器正在运行时(因此以某种方式检测它何时实际工作)并且每当它打开时,显示通知.每当它未运行时,应擦除通知.如果需要更多代码等,请发表评论,感谢您的帮助.

I would love to simply destroy the notification if the app is force closed etc, or if possible I would not mind the app to be a service so even when swiped from the recents, it will still run. The best scenario would be, however, for when my broadcast receiver is running (so somehow detect when it is actually working) and whenever it is on, show a notification. Whenever it is not running the notification should be wiped. If more code is needed etc just comment, thanks for any help guys.

推荐答案

据我所知,您正在创建一个新的 NotificationManager 对象来创建通知,并创建一个不同的 NotificationManager 对象来取消通知.您应该使用相同的对象.您这样做的方式是,您没有使用 notifDestroy() 中的对象触发任何通知,因此它可能不会为您清除通知.

As far as i can tell you are creating a new NotificationManager object to create the notification and a different notificationManager object to cancel the notifications. You should be using the same object. The way you are doing it, you are not firing any notification with the object in the notifDestroy(), hence probably it doesnt clear the notification for you.

这篇关于android 应用程序强制关闭后显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆