2012-11-28 18 views
0

我通过通知的意图发送一些数据(字符串)。但是,当我尝试通过通知发起的活动接收它时,我什么也收不到。这是我的代码。请帮忙。如何检索通过意图传递的数据?

这是通知代码:

int icon=R.drawable.ic_launcher; 
       String ticket="Encrypted message"; 
       long when=System.currentTimeMillis(); 
       NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
       Context context1 =getApplicationContext(); 
       String expandedText="You've received an encrypted message, click to view"; 
       String expandedTitle="Encrypted Message"; 
       Notification notification = new Notification(icon, ticket, when); 
       Intent intent1=new Intent(context,Try.class); 
       intent1.putExtra("MSG","Jumbo"); 
       PendingIntent launchIntent=PendingIntent.getActivity(context,0, intent1,0); 

notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent); 
       mNotificationManager.notify(1,notification); 

这里是我如何接受它在Try.class:

t = (TextView)findViewById(R.id.dec); 
     String d = ""; 
     Intent I = getIntent(); 
     d = I.getStringExtra("MSG"); 
     String text = "This is an example"; 
     t.setText(d); 

没有错误 - TextView的设置为什么都没有,当我运行它

回答

0

getIntent()将无法在那里工作,尝试重写您的活动中的onNewIntent方法。

0

而不是

Intent intent1=new Intent(context,Try.class); 

变化

Intent intent1=new Intent(YourActivity.this,Try.class); 

尝试在onCreate()方法

​​
+0

它在'onCreate()'方法 – Saturnian

0

你曾经调用 的setResult(Activity.RESULT_OK,意图) 你通知?您需要这样或Android将永远不会重新启动您的“通话”活动。

+0

不,我没有。我刚刚添加它,它说它不适用。 – Saturnian

+0

能否详细说明一下?你是否收到语法错误,运行时错误或什么? –

0

这应该工作,尝试登录“d”

Log.d("Value of D", d); 

我觉得你的问题是,你的TextView吨设置不正确,因此不显示结果的值。

+0

我设置了d =“displayText”并执行它 - 它运行良好。 TextView没问题!就是这个意图! :( – Saturnian