微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不停地更改android通知消息并再次启动服务

当我在 android上启动Service时,这是我的通知方法
private void showNotification() {
        Notification notification = new Notification(R.drawable.call,"Some text",System.currentTimeMillis());
        Intent intent = new Intent(this,MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this,intent,0);
        notification.setLatestEventInfo(this,getString(R.string.notification_label),getString(R.string.notification_text_short),pi);
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(7331,notification);
    }

以后可以更改文本吗?现在是例如“一些文本”,后来我想不停地更改它并再次启动服务.

那可能吗?

解决方法

您正在使用通知ID 7331显示通知以及启动服务.只要您发送具有相同ID的新通知,它就会替换旧的通知.

使用此选项更新通知

String ns = Context.NOTIFICATION_SERVICE;
notificationmanager mnotificationmanager = (notificationmanager) context
            .getSystemService(ns);
long when = System.currentTimeMillis();

Notification notification = new Notification("your icon","your ticker text",when);
/*<set your intents here>*/
mnotificationmanager.notify(7331,notification);

原文地址:https://www.jb51.cc/android/310027.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐