Android intent->活动互动

时间:2014-04-01 13:51:29

标签: android android-intent android-activity google-cloud-messaging

我有 一个gcm服务接收器,由书本完成。 这是主要部分

        //Magick. Create Intent that will show alert.
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent ma = new Intent(this, Home.class);
    Bundle b = ma.getExtras();
    if (b != null && b.containsKey("Alert_call"))
    {ma.removeExtra("Alert_call");}
    if (b != null && b.containsKey("Alert_poi"))
    {ma.removeExtra("Alert_poi");}

    ma.putExtra("Alert_call", true);
    ma.putExtra("Alert_poi", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,ma, 0);

    //Make the notification itself.
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(p.GetAlertIconFromType())
    .setContentTitle(p.GetAlertHeaderFromType())
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(p.coordsAdress + "\n" + p.ShortDesc))
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))//Do the ring
    .setDefaults(Notification.DEFAULT_VIBRATE)//Do the vibe
    .setDefaults(Notification.DEFAULT_LIGHTS)//Do the Lights blink
    .setContentText(p.coordsAdress + "\n" + p.ShortDesc);

    //Send.
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

现在,在活动方面,我正在抓住这个意图的捆绑包,看看我是否有这些“Alert_call”参数。如果是这样 - 使用那些ti显示一些特定的信息。

        //Magick. Create Intent that will show alert.
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent ma = new Intent(this, Home.class);
    Bundle b = ma.getExtras();
    if (b != null && b.containsKey("Alert_call"))
    {ma.removeExtra("Alert_call");}
    if (b != null && b.containsKey("Alert_poi"))
    {ma.removeExtra("Alert_poi");}

    ma.putExtra("Alert_call", true);
    ma.putExtra("Alert_poi", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,ma, 0);

    //Make the notification itself.
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(p.GetAlertIconFromType())
    .setContentTitle(p.GetAlertHeaderFromType())
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(p.coordsAdress + "\n" + p.ShortDesc))
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))//Do the ring
    .setDefaults(Notification.DEFAULT_VIBRATE)//Do the vibe
    .setDefaults(Notification.DEFAULT_LIGHTS)//Do the Lights blink
    .setContentText(p.coordsAdress + "\n" + p.ShortDesc);

    //Send.
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

- > DisplayView部分

        //Magick. Create Intent that will show alert.
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent ma = new Intent(this, Home.class);
    Bundle b = ma.getExtras();
    if (b != null && b.containsKey("Alert_call"))
    {ma.removeExtra("Alert_call");}
    if (b != null && b.containsKey("Alert_poi"))
    {ma.removeExtra("Alert_poi");}

    ma.putExtra("Alert_call", true);
    ma.putExtra("Alert_poi", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,ma, 0);

    //Make the notification itself.
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(p.GetAlertIconFromType())
    .setContentTitle(p.GetAlertHeaderFromType())
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(p.coordsAdress + "\n" + p.ShortDesc))
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))//Do the ring
    .setDefaults(Notification.DEFAULT_VIBRATE)//Do the vibe
    .setDefaults(Notification.DEFAULT_LIGHTS)//Do the Lights blink
    .setContentText(p.coordsAdress + "\n" + p.ShortDesc);

    //Send.
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

现在,我面临的问题是:

如果我从此警报启动活动 - 一切正常。 但是,如果我已经打开了这个活动(例如睡着了),并且我推动了这个警报 - 我在INtent或旧的(用于启动活动的那些)中接受null作为参数。

所以,问题是:

看起来我需要获取此运行的活动意图,并在推送警报时在其中添加新数据。

这是对的吗?如果是这样的话 - 请告诉我在哪里阅读它?

1 个答案:

答案 0 :(得分:0)

解决方案很简单......

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,ma, PendingIntent.FLAG_UPDATE_CURRENT);