In MainActivity I have a TextView: textV1. I also have a method in MainActivity that updates that textview:
public void updateTheTextView(final String t) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
TextView textV1 = (TextView) findViewById(R.id.textV1);
textV1.setText(t);
}
});
}
In a BroadcasrReceiver I need to update the text in textV1 in MainActivity.
public class NotifAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// other things done here like notification
// NEED TO UPDATE TEXTV1 IN MAINACTIVITY HERE
}
}
How can this be done? The BroadcastReceiver is run from a service. This code I cannot change. Can I access and change textV1 in MainActivity from onReceive()? I've tried many things but all fail.
create an instance of the class and then pass the value to the function that changes TextView value follow these steps please : in your BroadcastReceiver overRide onReceive method and paste These lines or changes theme as you wish
now we explain the updateTheTextView and inst in YourActivityToUpdate class Paste these Lines please
and this is the updateTheTextView method that should be placed in YourActivityToUpdate class
i think this is a better way thanks to "kevin-lynx"
In your
MainActivity
initialize a variable ofMainActivity
class like below.