作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 Honeycomb 开发,几天来我一直在努力解决这个问题。我有一个没有 Intent 的通知服务(不需要一个),问题是每次调用 displaymessage 函数后,每次都会弹出通知,所以我收到 100 个通知。我希望它只弹出一次,之后只更改百分比的文本。类似于从市场进度条和百分比下载。我已经隔离了该功能并创建了新的测试代码但没有成功。如果你从另一个角度看这个,我希望在不创建新通知的情况下更改现有通知的文本。你能帮帮我吗?
这是完整的代码(隔离之后):
package com.disp;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
public class DispalyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for (int i = 0; i < 100; i++) {
SystemClock.sleep(300);
displaymessage(""+i+"%");
}
}
public void displaymessage(String string) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, "Notification Service", System.currentTimeMillis());
Context context = getApplicationContext();
notification.setLatestEventInfo(context, "Downloading Content:", string, null);
final int HELLO_ID = 2;
mNotificationManager.notify(HELLO_ID, notification);
}
}
最佳答案
因为每个通知都由 NotificationManager 以整数 ID 唯一标识,您可以通过调用 setLatestEventInfo() 为新值修改通知,更改通知的某些字段值,然后再次调用 notify()。
您可以使用对象成员字段修改每个属性(上下文和通知标题和文本除外)。当您通过使用 contentTitle 和 contentText 的新值调用 setLatestEventInfo() 来更新通知时,您应该始终修改文本消息。然后调用 notify() 来更新通知。 (当然,如果您创建了自定义通知布局,则更新这些标题和文本值将无效。)
来自
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
关于Android Honeycomb 通知弹出过于频繁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9520190/
我是一名优秀的程序员,十分优秀!