通知上的“取消"按钮以删除通知

问题描述:

我有一个带有Progress BarCancel按钮的通知.我正在使用线程来增加进度栏,而取消"按钮应该清除通知.

I have a Notification that has a Progress Bar and a Cancel button. I am using Thread to increment the Progress Bar and the Cancel button is supposed to clear the notification.

这是通知的代码

    remoteViews = new RemoteViews(getPackageName(), R.layout.customnotification);
    Intent intent = new Intent(this, LocationBroadcastReceiver.class);
    intent.putExtra("CancelServiceAndNotification","CancelServiceAndNotification");
    intent.putExtra("ID",1);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
       0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.bCancel, pendingIntent);

使用线程增加进度栏的代码

Code for incrementing Progress Bar using Thread

     thread = new Thread(new Runnable() {
        @Override
        public void run() {
            int incr;
            for(incr = 0;incr<=100;incr+=10){
                remoteViews.setProgressBar(R.id.progressBar, 100, incr, false);
                if(cancelNotification){

                }
                notificationManager.notify(1,notification);
                try{
                    Thread.sleep(1000);
                }
                catch (Exception e){

                }

            }
            notificationManager.cancel(1);
        }
    });
    thread.start();

在广播接收器中,我正在尝试使用取消通知

In Broadcast Receiver, I am trying to cancel the notification using

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(ID);

问题是,当我单击Cancel按钮时,通知消失了一秒钟,然后又再次出现,可能是由于线程仍在运行.如何销毁广播Receiver类中的线程?我该怎么做才能删除cancel按钮单击上的通知?

The problem is that when I click on Cancel button the notification disappear for a second but then appear again, may be due to the thread still running. How to destroy the thread from broadcast Receiver class? What should I do to remove the notification on cancel button click ?

要停止线程,您应该中断它,例如,在BroadcastReceiver中收到"CancelServiceAndNotification"时,您可以调用:

To stop the thread you should interrupt it, for example when receiving "CancelServiceAndNotification" in your BroadcastReceiver you can call :

thread.interrupt();

然后,在您的run()方法中:

Then, in your run() method :

    if (Thread.currentThread().isInterrupted()) {
        // we were interrupted, should stop immediately
        return;
    }

如果线程在睡眠时被中断,它将抛出InterruptedException并清除中断的标志,因此对于您而言重要的是在捕获InterruptedException时从run()返回:>

If thread is interrupted while sleeping it will throw InterruptedException and clear interrupted flag, so in your case it is very important that you return from run() when catching InterruptedException :

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // we were interrupted, should stop immediately
        return;
    }

您应该以与您相同的方式取消通知:

And you should cancel notification the same way you do :

// make sure you pass 1 as id here as you use it for notify()
notificationManager.cancel(1);

应该可以.

但是,强烈建议不要从非主线程(在调用remoteViews.setProgressBar时从自定义线程执行操作)更新UI.

However, it is strongly not recommended to update UI from non-main thread (which you do from you custom thread when calling remoteViews.setProgressBar).

我建议使用自定义AsyncTaskdoInBackground中执行后台工作(而不是您的run()方法,并在其onProgressUpdate中更新通知的进度.
您可以通过调用cancel()来取消AsyncTask,方法与您使用interrupt()线程类似,如上所示.

I recommend using custom AsyncTask for doing background job in doInBackground (instead of your run() method and updating progress of your notification in its onProgressUpdate.
You can cancel AsyncTask by calling cancel() on it in a similar way you interrupt() a thread as shown above.

希望有帮助.