取消从多任务面板中删除应用程序的通知 [英] Cancel notification on remove application from multitask panel

查看:25
本文介绍了取消从多任务面板中删除应用程序的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理来自我的应用程序(而非来自服务)的持续通知.

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.

我的问题是:

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

更新:

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

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 是所有正在运行的活动的列表

activities is a list of all running activities

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

It's not simplest mechanism but I need it

我应该使用服务吗?

作为新的更新:

在我的 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)

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

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 ?!

如何强制正确终止?

推荐答案

当主应用被终止时终止通知.

由于您的通知和您的应用程序是在不同的线程中处理的,因此通过 MultitaskManager 杀死您的应用程序不会终止您的通知.由于您已经正确调查了杀死您的应用程序甚至不会导致 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);
    }
}

第 2 步:将其添加到您的清单中:将它添加到您的 <application> 之间的某个位置.标签.

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

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

第 3 步:始终在触发通知之前创建服务,并使用静态通知 ID.

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.

这篇关于取消从多任务面板中删除应用程序的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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