一个Service,发生某些事件后会生成notification。点击notification后会打开一个activity用于显示事件内容。
事件内容通过Intent的putExtra方法携带,在activity中用Intent.getExtra方法获得。
现在的问题是:发生了不同的事件后,service确实发送了不同的notification(从系统的通知栏里看,这些notification是不一样的,ticker也不一样),但点击以后,打开的activity,显示的内容都一样。可以确定的是:
1、生成的notification确实是不同的,id也不一样;
2、通过Intent携带的内容是不同的;我想这肯定是个小白问题,但自己已经搞了一上午了也没弄好,大家帮帮忙。
事件内容通过Intent的putExtra方法携带,在activity中用Intent.getExtra方法获得。
现在的问题是:发生了不同的事件后,service确实发送了不同的notification(从系统的通知栏里看,这些notification是不一样的,ticker也不一样),但点击以后,打开的activity,显示的内容都一样。可以确定的是:
1、生成的notification确实是不同的,id也不一样;
2、通过Intent携带的内容是不同的;我想这肯定是个小白问题,但自己已经搞了一上午了也没弄好,大家帮帮忙。
解决方案 »
- Android怎样得到系统设置的来电提示音
- EditText如何实现类似QQ聊天文本框里面的动态表情显示?
- android 图片合成视频
- 求助:android使用JNI修改Ethernet的IP地址和MAC地址不能成功。
- 谁能解释一下其中的机制!!getSystemService()??
- android 4.0以上socket开发问题
- android 如何获取U盘的目录?
- Andoird 环境下的串口通信
- 关于左边留间隔的问题marginLeft
- 有人知道怎么修改sharesdk框架的分享界面的样式吗?
- android不需要gps模块能定位吗?
- Android MediaRecord 录像问题
此时信息已经不能再通过onCreate来传递了,而是通过onNewIntent来传送。所以你可以通过重写onNewIntent函数来获取新的信息。
int mId = ...;
Intent i = new Intent();
i.putExtra("EXTRA_ID", mId);
i.setData(Uri.parse("content://data/test" + mId));Ok,这样传过去的id就符合期望了,别问我为什么。
这个是一种方案,使用相同Activity不去新建,简单的做法是每次都新建一个Activity,只要给Intent设置Flag就可以了,很好懂,如下:
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
Intent.FLAG_ACTIVITY_CLEAR_TOP)
这个是一种方案,使用相同Activity不去新建,简单的做法是每次都新建一个Activity,只要给Intent设置Flag就可以了,很好懂,如下:
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
Intent.FLAG_ACTIVITY_CLEAR_TOP)
private void logNotification(String tick, String title, String info)
{
String service = Context.NOTIFICATION_SERVICE;
NotificationManager notiMgr = (NotificationManager) getSystemService(service);
Context context = this;
String contentTitle = title;
String contentText = info;
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra("title", contentTitle);
intent.putExtra("content", contentText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification();
noti.icon = R.drawable.ic_launcher;
noti.tickerText = tick;
noti.when = System.currentTimeMillis();
noti.defaults |= Notification.DEFAULT_SOUND;
noti.flags = Notification.FLAG_AUTO_CANCEL;
noti.setLatestEventInfo(context, contentTitle, contentText, pi);
notiMgr.notify(RandomFactory.getInstance().nextInt(), noti);
}其中的NotificationActivity用于显示事件信息,在onCreate方法中通过Intent获取信息标题和内容,并显示在一个TextView中。大家帮忙看看,到底咋回事儿。
String message, Intent intent) {
Notification notification = new Notification(R.drawable.tj_crm,
message, System.currentTimeMillis());
// 自动清除
notification.flags = Notification.FLAG_AUTO_CANCEL;
// notification.defaults = Notification.DEFAULT_SOUND;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Random random = new Random();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.addFlags(Intent.FILL_IN_DATA);
intent.putExtra("info_id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
random.nextInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, title, message, pendingIntent);
notificationMgr.notify(random.nextInt(), notification);
}
能解释一下你的理解吗?
int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
楼上说的不正确,经过测试,每次都会生成新的Activity,就是因为每次生成了新的activity,现在造成BUG
http://blog.csdn.net/h3c4lenovo/article/details/7718648