【问题标题】:Notification shows after android app is force closedandroid应用程序强制关闭后显示通知
【发布时间】:2014-02-09 03:22:37
【问题描述】:

我今天不停地用谷歌搜索,但找不到任何东西。我的情况如下:

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

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;
}

我的目标如下:

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

【问题讨论】:

    标签: android notifications


    【解决方案1】:

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

    【讨论】:

    • 你介意给我一个例子来说明如何做到这一点吗?我这样做的方式是我知道的唯一方式。谢谢(:
    • 我要说的是使用与在 notifDestroy 中创建通知相同的对象。删除 NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);从notifDestroy方法中取出notifManager,让notifManager成为一个类级别的变量,这样所有的方法都可以访问它
    • 好的,这就是我认为你的意思,但请确保,我会尽快尝试,如果可行,我会接受你的回答,再次感谢您的帮助。
    • 即使您从最近面板关闭应用程序,通知仍保留在状态栏中。那是因为您可能正在清单中注册广播接收器。
    • 我不应该在清单中注册广播接收器吗?
    【解决方案2】:

    当我从最近的应用程序中滑动应用程序时,我用来杀死进程的方法是:在应用程序的第一个活动中,在 OnDestroy 方法中我写了“进程必须杀死”。通过这种方式,我调用了进程的 OnDestroy 方法,因此进程真的杀死了自己,并且通知被删除了。 具体来说,在第一个 Activity 中:

        @Override
    public void onDestroy() {
      super.onDestroy();
    
      Intent intent = new Intent();
      intent.setAction(Service.MY_ACTION_FROMACTIVITY);
      intent.putExtra(Service.CMD, Service.CMD_STOP);
      sendBroadcast(intent);
    
    }
    

    所以下面的操作是:

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        barNotification.cancel(NOTIFICATION);
    
        sensorManager.unregisterListener(this);
    
        this.unregisterReceiver(myServiceReceiver);
        super.onDestroy();
    }
    

    希望能帮到你。

    【讨论】:

      【解决方案3】:

      如果您的通知由服务处理且目标 API 为 14 或更高,那么您有机会在覆盖的“void onTaskRemoved(Intent rootIntent)”方法中取消通知。

      【讨论】:

        【解决方案4】:

        您是否在通知中尝试.setAutoCancel(true),并在活动xml 中添加android:launchMode="singleTask"android;clearTaskOnLauch="true"

        您的通知图标应该会在您按下通知栏的那一刻清除。

        【讨论】:

          猜你喜欢
          • 2015-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-17
          • 2021-09-23
          相关资源
          最近更新 更多