如何将参数从通知单击发送到活动?

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


我有一个创建通知的服务。当用户单击通知时,我想使用一些特殊参数打开我的主要活动。例如,一个项目ID,这样我的活动就可以加载并显示一个特殊的项目详细信息视图。更具体地说,我正在下载文件,下载文件时,我希望通知具有以下意图:单击该通知后,它将以特殊模式打开我的活动。我尝试使用putExtra我的意图,但似乎无法提取它,因此我认为我做错了。


我的服务中创建通知的代码:


        // 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);

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


 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") );

    }

Extras始终为null,并且我从不记录任何内容。


顺便说一句... onCreate仅在我的活动开始时运行,如果我的活动已经开始,我还想收集额外的物品,并根据收到的item_id介绍我的活动。


有任何想法吗?


达令说
浏览 380回答 3
3回答

一只名叫tom的猫

我遇到类似的问题,我的应用程序显示消息通知。当有多个通知并单击每个通知时,它将在查看消息活动中显示该通知详细信息。我解决了在视图消息意图中接收到相同额外参数的问题。这是修复此问题的代码。用于创建通知意图的代码。&nbsp;Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);&nbsp; &nbsp; notificationIntent.putExtra("NotificationMessage", notificationMessage);&nbsp; &nbsp; notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);&nbsp; &nbsp; PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);&nbsp; &nbsp; notification.flags |= Notification.FLAG_AUTO_CANCEL;&nbsp; &nbsp; notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);查看消息活动的代码。@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; onNewIntent(getIntent());}@Overridepublic void onNewIntent(Intent intent){&nbsp; &nbsp; Bundle extras = intent.getExtras();&nbsp; &nbsp; if(extras != null){&nbsp; &nbsp; &nbsp; &nbsp; if(extras.containsKey("NotificationMessage"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.viewmain);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // extract the extra-data in the Notification&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String msg = extras.getString("NotificationMessage");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txtView = (TextView) findViewById(R.id.txtMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txtView.setText(msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android