So, I have my application which has BroadcastReceiver and receives notification from other application. The problem is, I am receiving a message, displaying it in the phone via Toast, but I want to set it visible on my TextView. I have only two classes in my whole app.
My receiver is my own class which extends BroadcastReceiver and its onReceive method looks like this:
@Override
public void onReceive(Context context, Intent intent) {
String value = intent.getExtras().getString("hiddenMessage");
MainActivity.getInstace().updateTheTextView(value);
}
Here's the code of the method which updates textView. This method is in MainActivity class:
public void updateTheTextView(final String textFromNotification) {
final Context context = this;
Toast toast = Toast.makeText(context, "Message received: " + textFromNotification, Toast.LENGTH_LONG);
toast.show();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
textV1.setText(textFromNotification);
}
});
}
The problem is, when I send a notification from one app and receive it here, text is not visible. It's only visible when this app is open. How can I set text to field when my app is not open? Store message to some class field and use setText() in onResume method?