如何将参数从通知点击发送到活动? [英] How to send parameters from a notification-click to an activity?

查看:19
本文介绍了如何将参数从通知点击发送到活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到一种方法从通知中向我的活动发送参数.

我有一个创建通知的服务.当用户点击通知时,我想用一些特殊参数打开我的主要活动.例如一个项目 ID,所以我的活动可以加载并呈现一个特殊的项目详细信息视图.更具体地说,我正在下载一个文件,当下载文件时,我希望通知有一个意图,即在单击时它会以特殊模式打开我的活动.我曾尝试按照我的意图使用 putExtra,但似乎无法提取它,所以我认为我做错了.

来自我的服务的代码,用于创建通知:

//构造通知对象.final Notification notif = new Notification(R.drawable.icon,tickerText, System.currentTimeMillis());final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);contentView.setImageViewResource(R.id.image, R.drawable.icon);contentView.setTextViewText(R.id.text,tickerText);contentView.setProgressBar(R.id.progress,100,0, false);notif.contentView = contentView;Intent notificationIntent = new Intent(context, Main.class);notificationIntent.putExtra("item_id", "1001");//<-- 在这里我放了额外的值PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);notif.contentIntent = contentIntent;nm.notify(id, notif);

我的活动中尝试从通知中获取额外参数的代码:

 public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);捆绑额外费用 = getIntent().getExtras();如果(额外的!= null){Log.i("dd","Extra:" + extras.getString("item_id"));}

附加内容始终为空,我从不将任何内容写入我的日志中.

顺便说一句... onCreate 仅在我的活动开始时运行,如果我的活动已经开始,我还想收集额外内容并根据我收到的 item_id 展示我的活动.

有什么想法吗?

解决方案

看看这个指南 (创建通知) 和示例 ApiDemos "StatusBarNotifications" 和 "NotificationDisplay".

要管理活动是否已经在运行,您有两种方法:

  1. 在启动 Activity 时将 FLAG_ACTIVITY_SINGLE_TOP 标志添加到 Intent 中,然后在 Activity 类中实现 onNewIntent(Intent intent) 事件处理程序,这样您就可以访问为活动调用的新意图(这与仅调用 getIntent() 不同,这将始终返回启动您的活动的第一个意图.

  2. 与第一个相同,但不是向 Intent 添加标志,您必须在您的活动 AndroidManifest.xml 中添加 "singleTop".

如果您使用 Intent extras,请记住使用标志 PendingIntent.FLAG_UPDATE_CURRENT 调用 PendingIntent.getActivity(),否则每个通知都会重复使用相同的 extras.

I can find a way to send parameters to my activity from my notification.

I have a service that creates a notification. When the user clicks on the notification I want to open my main activity with some special parameters. E.g an item id, so my activity can load and present a special item detail view. More specific, I'm downloading a file, and when the file is downloaded I want the notification to have an intent that when clicked it opens my activity in a special mode. I have tried to use putExtra on my intent, but cant seem to extract it, so I think I'm doing it wrong.

Code from my service that creates the Notification:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, tickerText);
    contentView.setProgressBar(R.id.progress,100,0, false);
    notif.contentView = contentView;        

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;

    nm.notify(id, notif);

Code from my Activity that tries to fetch the extra parameter from the notification:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + extras.getString("item_id") );
    }

The extras is always null and I never gets anything into my log.

Btw... the onCreate is only run when my activity starts, if my activity is already started I also want to collect the extras and present my activity according to the item_id I receive.

Any ideas?

解决方案

Take a look at this guide (creating a notification) and to samples ApiDemos "StatusBarNotifications" and "NotificationDisplay".

For managing if the activity is already running you have two ways:

  1. Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

  2. Same as number one, but instead of adding a flag to the Intent you must add "singleTop" in your activity AndroidManifest.xml.

If you use intent extras, remeber to call PendingIntent.getActivity() with the flag PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every notification.

这篇关于如何将参数从通知点击发送到活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆