一个Service,发生某些事件后会生成notification。点击notification后会打开一个activity用于显示事件内容。
事件内容通过Intent的putExtra方法携带,在activity中用Intent.getExtra方法获得。
现在的问题是:发生了不同的事件后,service确实发送了不同的notification(从系统的通知栏里看,这些notification是不一样的,ticker也不一样),但点击以后,打开的activity,显示的内容都一样。可以确定的是:
1、生成的notification确实是不同的,id也不一样;
2、通过Intent携带的内容是不同的;我想这肯定是个小白问题,但自己已经搞了一上午了也没弄好,大家帮帮忙。

解决方案 »

  1.   

    如果要显示的信息的Activity已经存在了,系统不会再生成一个新的Activity,而是会用原来的Activity来显示
    此时信息已经不能再通过onCreate来传递了,而是通过onNewIntent来传送。所以你可以通过重写onNewIntent函数来获取新的信息。
      

  2.   

    putExtra很奇怪,之前遇到过同样的问题,可以再给它setData来解决,比如:
    int mId = ...;
    Intent i = new Intent();
    i.putExtra("EXTRA_ID", mId);
    i.setData(Uri.parse("content://data/test" + mId));Ok,这样传过去的id就符合期望了,别问我为什么。
      

  3.   


    这个是一种方案,使用相同Activity不去新建,简单的做法是每次都新建一个Activity,只要给Intent设置Flag就可以了,很好懂,如下:
    Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
                    Intent.FLAG_ACTIVITY_CLEAR_TOP)
      

  4.   


    这个是一种方案,使用相同Activity不去新建,简单的做法是每次都新建一个Activity,只要给Intent设置Flag就可以了,很好懂,如下:
    Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
                    Intent.FLAG_ACTIVITY_CLEAR_TOP) 
      

  5.   

    不知道为啥,现在如果有几个notification在通知栏上,不管点击那个,打开的activity中,都显示的是最后一个事件的信息。
      

  6.   

    我把代码粘出来//创建notification的方法
    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中。大家帮忙看看,到底咋回事儿。
      

  7.   

    我知道  怎么做 只是我语文太差  所以  建议楼主先去 百度或是找android程序运行的四种模式 看看 就知道了  是在android**main.xml 里面设置的 
      

  8.   

    private void displayNotificationMessage(String id, String title,
    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);
    }
      

  9.   

    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP
    能解释一下你的理解吗?
      

  10.   

    因为你后面的intent把前面的覆盖掉了。你给intent设置不同的action区分开应该就可以了int x = random.next();intent.setAction(x + "");
      

  11.   

    这个问题我遇到过,首先2楼的方法说的是对的,你需要重写onNewIntent,如何能进入onNewIntent方法?需要设置intent的flag FLAG_ACTIVITY_SINGLE_TOP楼主看看进入到 onNewIntent方法没,如果进入了,直接调用onNewIntent的参数intent就可以获取到新的数据了。但是单是这样还是不够的,PendingIntent pi = PendingIntent.getActivity(context, 0, intent, int FLAG_UPDATE_CURRENT);最后这个参数应设置成int FLAG_UPDATE_CURRENT,这样才可以将intent的数据更新
    int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
    int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
    int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
    int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
      

  12.   


    楼上说的不正确,经过测试,每次都会生成新的Activity,就是因为每次生成了新的activity,现在造成BUG
      

  13.   

    思考这个问题时,查到这样一遍文章,与诸君共享
    http://blog.csdn.net/h3c4lenovo/article/details/7718648