如何从一个通知,点击活动送的参数? [英] How to send parameters from a notification-click to an activity?

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

问题描述

我能找到一种方法,从我的通知参数发送到我的活动。

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

我有一个创建一个通知的服务。当用户点击通知我想用一些特殊的参数,打开我的主要活动。例如一个项目的ID,所以我的活动可以加载和present一个特殊的项目详细信息视图。更具体的,我下载的文件,并下载该文件时,我想通知有意向的点击,当它打开我的活动在一个特殊的模式。我曾尝试使用 putExtra 在我的意图,但不能似乎提取它,所以我觉得我做错了。

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,创建通知:

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,试图获取从通知的额外的参数:

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.

顺便说一句...的的onCreate 只运行我的活动开始的时候,如果我的活动已经开始我也想收集群众演员和present我的活动根据ITEM_ID我收到。

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.

任何想法?

推荐答案

看看这个导游(的创建一个通知)和样品ApiDemosStatusBarNotifications和NotificationDisplay。

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. 添加 FLAG_ACTIVITY_SINGLE_TOP 标记的意图开展活动时,然后在活动课实施 onNewIntent(意向意图)事件处理程序,这样你可以访问新意图被要求的活动是(这是不一样的只是调用getIntent(),这将始终返回启动你的活动的第一个意图

  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.

相同,而不是把一个标志的意图,你必须添加作为一把手,但singleTop在活动AndroidManifest.xml中。

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

如果您使用的意图群众演员,要的记住叫 PendingIntent.getActivity()的标志 PendingIntent.FLAG_UPDATE_CURRENT ,否则,同样的演员将被重新用于每个通知。

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天全站免登陆