取消有关从多任务面板中删除应用程序的通知

2023-11-25

我管理一个ONGOING来自我的应用程序的通知(而不是来自服务)。

当我使用“结束”按钮从任务管理器终止应用程序时,通知消失.

当我从多任务面板中删除应用程序时,应用程序被终止,但是通知仍然存在.

我的问题是:

  • 如何捕获此事件以清除通知?
  • 当应用程序从多任务面板中删除时会发生什么?应用程序被破坏但进程仍然存在?正常吗?

作为更新:

我的所有活动都使用以下方法扩展了 MyActivity 类(它扩展了 Activity):

@Override protected void onCreate(Bundle state) {
    super.onCreate(state);
    ((MyApplication) getApplication()).onActivityCreate(this, state);
}

@Override protected void onDestroy() {
    super.onDestroy();
    ((MyApplication) getApplication()).onActivityDestroy(this);
}

我的应用程序使用方法扩展了 MyApplication 类(它扩展了 Application):

private List<Activity> activities = new ArrayList<Activity>();

protected final void onActivityCreate(Activity activity, Bundle state) {
    if(activities.isEmpty() && state == null) {
        onStart();
    }
    activities.add(activity);
}

protected final void onActivityDestroy(Activity activity) {
    activities.remove(activity);
    if(activities.isEmpty() && activity.isFinishing()) {
        onExit();
    }
}

protected void onStart() {
    // some code
}

protected void onExit() {
    // some code
    notificationManager.cancel(NOTIFICATION_ID);
}

activities是所有正在运行的活动的列表

这不是最简单的机制,但我需要它

我应该使用服务吗?


作为新的更新:

在我的 onExit() 方法中,如果我记录调试消息以了解会发生什么,如下所示:

public void onExit() {
    for(int i = 0; i < 100; i++) {
        Log.d(TAG, "onExit");
    }
}

少量日志出现在两次,而不是全部(例如:13/100)

所以,我知道从多任务面板中删除应用程序会强制终止应用程序,而无需等待关闭方法结束正确完成...但为什么不处理?!

我怎样才能强制正确终止?


主应用程序被终止时的终止通知。

由于您的通知和应用程序是在不同的线程中处理的,因此通过 MultitaskManager 终止您的应用程序不会终止您的通知。正如您已经正确调查的那样,杀死您的应用程序甚至不一定会导致 onExit() 回调。

那么解决办法有哪些呢?

您可以从您的活动中启动服务。特殊服务具有:如果应用程序进程由于某种原因被终止,它们会自动重新启动。因此,您可以通过终止重新启动时的通知来重用自动重新启动。

第1步创建一个杀死的服务简单的一个。它只是杀死创建时的通知并有他特殊的活页夹。

public class KillNotificationsService extends Service {

    public class KillBinder extends Binder {
        public final Service service;

        public KillBinder(Service service) {
            this.service = service;
        }

    }

    public static int NOTIFICATION_ID = 666;
    private NotificationManager mNM;
    private final IBinder mBinder = new KillBinder(this);

    @Override
    public IBinder onBind(Intent intent) {
            return mBinder;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            return Service.START_STICKY;
    }
    @Override
    public void onCreate() {
            mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNM.cancel(NOTIFICATION_ID);
    }
}

步骤2:将其添加到您的清单中:将其添加到 标记之间的某个位置。

<service android:name="KillNotificationsService"></service>

步骤3:始终在触发通知之前创建服务,并使用静态notificationid。

ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,
            IBinder binder) {
        ((KillBinder) binder).service.startService(new Intent(
                MainActivity.this, KillNotificationsService.class));
        Notification notification = new Notification(
                R.drawable.ic_launcher, "Text",
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(MainActivity.this,
                Place.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(),
                "Text", "Text", contentIntent);
        NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNM.notify(KillNotificationsService.NOTIFICATION_ID,
                notification);
    }

    public void onServiceDisconnected(ComponentName className) {
    }

};
bindService(new Intent(MainActivity.this,
        KillNotificationsService.class), mConnection,
        Context.BIND_AUTO_CREATE);

重新启动服务可能需要一些时间(1-5 秒),但它最终会启动并终止通知。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

取消有关从多任务面板中删除应用程序的通知 的相关文章

随机推荐