Уведомление появляется снова после отмены - PullRequest
1 голос
/ 24 февраля 2011

В моем сервисе есть уведомление, которое я отменяю в моем onDestroy. Уведомление немедленно появляется после выполнения кода отмены. Есть какие-нибудь подсказки? Я перепробовал все комбинации флагов. Нет радости Код, отредактированный для краткости, здесь.

public class downservice extends Service{
    Notification notification;
    RemoteViews contentView;
    private static final int notifyid = 1;
    Context context;
    NotificationManager mNM;
    PendingIntent cintent;

    @Override
    public void onCreate() {
        String ns = Context.NOTIFICATION_SERVICE;
        mNM = (NotificationManager)getSystemService(ns);        
        Intent noteintent = new Intent(this, configact.class);
        cintent = PendingIntent.getActivity(this, 0, noteintent, 0);
        contentView= new RemoteViews(getPackageName(), R.layout.notify);
        Message msgtx=Message.obtain();
        handler.sendMessage(msgtx);
    }
    private void showNotification(long[] data) {

        notification= new Notification();
        notification.contentIntent = cintent;
        notification.icon=R.drawable.notify;
        notification.iconLevel=x;
        notification.flags|=Notification.FLAG_AUTO_CANCEL;        
        contentView.setImageViewResource(R.id.notifyimage, R.drawable.notifyimage);        
        contentView.setTextViewText(R.id.notifytext,text);
        notification.contentView = contentView;
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(notifyid, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        mNM.cancel(notifyid);
    }


    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
                    Message msgtx=Message.obtain();
                    msgtx.arg1=1;
                    long [] data=getdata();
                    showNotification(data);             
                    handler.sendMessageDelayed(msgtx, updaterate*1000);
        }
    };  

1 Ответ

0 голосов
/ 02 марта 2011

Обработчик продолжает выполняться даже после того, как служба разрушена, и, следовательно, уведомление, которое находится в цикле обработчика, вновь появляется.Я изменил код, чтобы цикл обработки не продолжался после onDestroy ().Я также реализовал Handler.Callback, поскольку он более чистый, чем встроенный код.

@Override
    public boolean handleMessage(Message arg0) {
        switch(arg0.arg1){
            case 1: 
                Message msgtx=Message.obtain();
                msgtx.arg1=loopstatus;              
                long [] data=getdata();
                showNotification(data);             
                handler.sendMessageDelayed(msgtx, updaterate*1000);
                break;
            case 2:
                mNM.cancel(notifyid);
                break;
            default:
                break;
        }
        return true;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...