取消通知,从多任务面板中删除应用程序应用程序、面板、任务、通知

2023-09-12 07:48:53 作者:咬一口星星奶酪

我从我的应用程序(而不是从服务)管理的持续的通知。

I manage an ONGOING notification from my application (not from a service).

当我杀了从任务管理器应用程序结束按钮,的通知消失

When I kill application from task manager with "End" button, notification disappear.

当我从多任务潘内尔删除应用程序,应用程序被杀死,但的通知仍然

When I remove application from multitask pannel, application is killed but notification remains.

我的问题是:

如何捕捉这一事件明确通知? 当应用程序从多任务潘内尔删除在会发生什么?应用程序被破坏,但过程活着?它是正常的吗? How to catch this event to clear notification? What happens when application is removed from multitask pannel? Application is destroyed but process staying alive? Is it normal?

作为更新:

我所有的活动扩展MyActivity类(延伸活动)与方法:

All my activities extends MyActivity class (which extends Activity) with methods:

@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类(扩展应用)与方法:

And my application extends MyApplication class (which extends Application) with methods:

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 is a list of all running activities

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

It's not simplest mechanism but I need it

我应该使用一个服务,而不是?

Should I use a service instead?

作为一个新的更新:

在我onExit()方法,如果我登录调试消息知道发生了什么这样的:

In my onExit() method, if I Log debug message to know what happens like this:

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

少量日志上的两个出现一次,不是所有的(例如:13/100)

a small amount of log appears once on two, not all (ex: 13/100)

所以,据我所知,去除多任务潘内尔力应用程序结束应用程序,而无需等待close方法最终完成正常......但是,为什么不处理?!

So, I understand that remove application from multitask pannel force to kill application without waiting close methods end to finish properly... But why not process ?!

我怎么能强制终止正确?

How can I force to terminate properly ?

推荐答案

杀通知时,主要的应用程序已被杀死。

由于您的通知和您的应用程序在不同的线程通过MultitaskManager杀死你的应用程序不会杀你的通知处理。正如你已经正确地调查杀害你的应用程序甚至不会necesarrily导致onExit()回调。

Since your notification and your app are handled in different threads killing your app via MultitaskManager won't kill your notification. As you already correctly investigated killing your app won't even necesarrily result in an onExit() callback.

那么什么是解决方案?

您可以从活动启动服务。有个特别的服务有:它们会自动重新启动自己,如果应用进程已被杀害的某些原因。所以,你可以通过在重新启动查杀通知重用自动重启。

You could start a service from your activity. A specialty services have: they restart themselves automatically if app-process have been killed for some reason. So you could reuse the automatic restart by killing the notification on restart.

第1步创建一个杀死的服务 简单的。它只是杀死上创建一个通知,并有他的特殊粘合剂。

Step 1 create a service that kills Simple one. It just kills a notification on create and has his special Binder.

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

第二步:将它添加到你的清单: 添加到某处插图中的&lt;应用&GT;标签。

Step2: Add it to your manifest: Add it somewhere inbetween your <application> tags.

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

第三步:fireing通知之前,请务必创建服务,并使用静态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秒),但最终会启动并杀死的通知。

It might take a little time until service is restarted (1-5 sec), but it will eventually start and kill the notification.