[英]How to get value from service to activity in android?
Anyone please help me how to do that. 有人请帮我怎么做。
I have a simple android application in that application i am receiving a notification throw service at the time of clicking the notification i am opening the new class in that class i want to display the notification text in textview 我在该应用程序中有一个简单的android应用程序,在单击通知时我正在接收通知抛出服务,我正在该类中打开新类,我想在textview中显示通知文本
My doubt is how to pass notification text to activity 我的疑问是如何将通知文本传递给活动
Thanks in advance 提前致谢
You would be starting the activity using startActivity(intent);
您将使用startActivity(intent);
开始活动startActivity(intent);
Now before you do that, add intent.putExtraString(key,value)
. 现在,在执行此操作之前,添加intent.putExtraString(key,value)
。 And inside activity, get the passed string using getIntent().getStringExtra(key)
在内部活动中,使用getIntent().getStringExtra(key)
获取传递的字符串
To view message in activity and set to textView, for notification 要查看活动中的消息并将其设置为textView,以进行通知
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("NotificationMessage"))
{
setContentView(R.layout.notification_sample);
String message = extras.getString("NotificationMessage");
textView = (TextView) findViewById(R.id.textMessage);
textView.setText(message);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.