简体   繁体   中英

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

My doubt is how to pass notification text to activity

Thanks in advance

You would be starting the activity using startActivity(intent); Now before you do that, add intent.putExtraString(key,value) . And inside activity, get the passed string using getIntent().getStringExtra(key)

To view message in activity and set to textView, for notification

@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);
        }
    }
}

refer to this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM