卸下服务正在进行的通知 [英] Remove ongoing notification from service

查看:163
本文介绍了卸下服务正在进行的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有创建启动时通知的服务。

和则的OnDestroy()我希望它被删除。

我只是用.cancel(NOTIFICATION_ID);

它的工作原理伟大的,当它是一个正常的通知,但是当我使用正在进行的事件,它只是不会取消它。

我看过一些关于Android的,如果有资源为它服务不会停止,但如何克服呢?

我用这个code启动服务

 最终意图bg_service =新意图(BackgroundService.class.getName());    btn_start =(按钮)findViewById(R.id.btn_start);
    btn_stop =(按钮)findViewById(R.id.btn_stop);    btn_start.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            startService(bg_service);
        }
    });    btn_stop.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            stopService(bg_service);
        }
    });

我用这个在创建

  notificationManager =(NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);    通知通知=新的通知(R.drawable.ic_launcher,
            新的通知,System.currentTimeMillis的());    notification.flags | = Notification.FLAG_FOREGROUND_SERVICE;    意向意图=新意图(这一点,mainapp.class);
    的PendingIntent活性= PendingIntent.getActivity(在此,0,意图,0);
    notification.setLatestEventInfo(这一点,...,
            ...,活动);
    notificationManager.notify(19314,通知);

这在破坏

  noficationManager.cancel(19314);


解决方案

我想看看,如果你能这样做正在进行的通知略有不同。有迹象表明,生活在服务专用于添加和删除一个正在进行的通知方法。

<一个href=\"http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29\"相对=nofollow> Service.startForeground(INT,通知)

Service.stopForeground(布尔)

也许你可能只是尝试调用 stopForegroud(布尔)的onDestroy()方法首先...

I have a service that create a notification when it is started.

And then ondestroy() i want it to be removed.

I just use .cancel(NOTIFICATION_ID);

It works great when it is a normal notification but when i am using ongoing event it just won't cancel it.

I did read something about that services doesn't stop if android have the resources for it, but how to overcome this?

I use this code to start the service

    final Intent bg_service = new Intent(BackgroundService.class.getName());

    btn_start = (Button)findViewById(R.id.btn_start);
    btn_stop = (Button)findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(bg_service);
        }
    });

    btn_stop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(bg_service);
        }
    });

I use this in on create

            notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    Intent intent = new Intent(this, mainapp.class);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "...",
            "...", activity);
    notificationManager.notify(19314, notification);

And this in on destroy

            noficationManager.cancel(19314);

解决方案

I would look at doing 'ongoing' notifications a bit differently if you can. There are methods that live on Service that are dedicated to adding and removing an 'ongoing' notification.

Service.startForeground(int, Notification)

Service.stopForeground(boolean)

Perhaps you could just try calling stopForegroud(boolean) from your onDestroy() method first...

这篇关于卸下服务正在进行的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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