Krishna
Krishna

Reputation: 4984

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

Upvotes: 1

Views: 2035

Answers (2)

Amit Vaghela
Amit Vaghela

Reputation: 22965

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

Upvotes: 0

Aniket Awati
Aniket Awati

Reputation: 1421

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)

Upvotes: 1

Related Questions