强制关闭android应用后显示通知

强制关闭android应用后显示通知

问题描述:

我已经用Google搜索了这个不间断的网站,找不到任何东西.我的情况如下:

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.