且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-20 19:44:22

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

由于您的通知和您的应用程序是在不同的线程中处理的,因此通过 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.