Android BroadcastReceiver onReceive Update TextView in MainActivity

update ui from broadcastreceiver android
update textview from service android
date change broadcast receiver android
update ui from service android
how to call broadcast receiver from activity in android
android broadcastreceiver in activity
how to send data from activity to broadcastreceiver in android
how to test broadcast receiver in android

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.

In your MainActivity initialize a variable of MainActivity class like below.

public class MainActivity extends Activity {
    private static MainActivity ins;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ins = this;     
    }

    public static MainActivity  getInstace(){
        return ins;
    }

    public void updateTheTextView(final String t) {
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                TextView textV1 = (TextView) findViewById(R.id.textV1);
                textV1.setText(t);
            }
        });
    }
}


public class NotifAlarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            MainActivity  .getInstace().updateTheTextView("String");
        } catch (Exception e) {

        }           
    }
}

Android BroadcastReceiver onReceive Update TextView in , In MainActivity I have a TextView: textV1. I also have a method in MainActivity that updates that textview: public void updateTheTextView(final String t)  The MainActivity may not be alive when you receive a broadcast. There are two way you can get around this issue, 1) save the message in shared preference, and read it in MainActivity 2) Use Handler/Message class to pass the value see

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

private Handler handler = new Handler(); // Handler used to execute code on the UI thread
// Post the UI updating code to our Handler
handler.post(new Runnable() {
    @Override
    public void run() {
    //Toast.makeText(context, "Toast from broadcast receiver", Toast.LENGTH_SHORT).show();
    YourActivityToUpdate.updateTheTextView(message);
    YourActivityToUpdateinst = YourActivityToUpdate.instance();
        if(inst != null)  { // your activity can be seen, and you can update it's context
        inst.updateTheTextView(message);
        }
    }
});

now we explain the updateTheTextView and inst in YourActivityToUpdate class Paste these Lines please

private static SignUpVerify mInst;

public static SignUpVerify instance() {
        return mInst;
}
@Override
public void onStart() {
    super.onStart();
    mInst = this;
}

@Override
public void onStop() {
    super.onStop();
    mInst = null;
}

and this is the updateTheTextView method that should be placed in YourActivityToUpdate class

public void updateTheTextView(final String verifyCodeValue) {
                Log.i("verifyCodeValue", verifyCodeValue);
                YourTextViewToUpdate.setText(verifyCodeValue);
    }

i think this is a better way thanks to "kevin-lynx"

How to update UI in a BroadcastReceiver, public class MainActivity extends Activity { . public static class NissanTabBroadcast extends BroadcastReceiver { @Override public void onReceive(Context  Create MainActivity.java class inside your package in java folder. This is the Launcher Activity(start when you click on the app icon). In the MainActivity create an inner class MyBroadCastReceiver it will receive specified action fired from service.

java, En su MainActivity inicialice una variable de clase MainActivity como a continuación. public class MainActivity extends Activity { private static MainActivity ins;  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?

If someone is searching this exact solution, but in Kotlin, do the following:

class MainActivity : AppCompatActivity() {

    companion object {
        var ins: MainActivity? = null
        fun getInstance(): MainActivity? {
            return ins
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ins = this
    }

    fun updateTheTextView(t: String) {
        this@MainActivity.runOnUiThread {
            val textV1 = findViewById<TextView>(R.id.textV1)
            textV1.text = t
        }
    }
}

class NotifAlarm : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        try {
            MainActivity.getInstance()?.updateTheTextView("The String")
        } catch (e: Exception) {

        }
    }
}

How to update ui from Intent Service in android?, MainActivity"> <TextView android:id = "@+id/text" new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent)  public MainActivity extends AppCompatActivity implements MyBroadcastListener{ private BroadcastReceiver receiver; @Override public void onCreate(Bundle sis){ // Related code receiver = new MyReceiver(this); // This is how you initialise receiver // Register receiver etc } public void updateUI(String msg) { TextView textView = (TextView

In your broadcastreceiver class send broadcast

public class mybroadcaster extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    context.sendBroadcast(new Intent("updatetext"));
  }
}

In your activity register your broadcastreceiver and call it, do your work at onReceive and unregister the broadcaster in onDestroy()

public class MyActivity  extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    registerReceiver(broadcastReceiver, new IntentFilter("updatetext"));
}

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do your work here
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver);
  }
}

How do I update my activity using a BroadcastReceiver?, Android. I asked a similar question earlier and received some help, but I'm still stuck and I I want to update the text of a textview when the power is plugged or unplugged. public class MainActivity : Activity BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { if (intent. Android BroadcastReceiver onReceive Actualizar TextView en MainActivity. En MainActivity tengo un TextView: textV1. También tengo un método en MainActivity que actualiza que textview:

Using Broadcast Intent in DataCapture Profiles, Using EMDK Wizard and EMDK for Android Broadcast Intents. Next we will update "MainActivity", adding a button to launch our "BroadcastIntentActivity". android:layout_marginBottom="50dp" android:layout_marginLeft="120dp" android:text="Broadcast Intent" " /> Override "onReceive" inside the Broadcast Receiver. How to use snackbar in Android? How to register a BroadcastReceiver programmatically in Android? How to display toast messages from a thread in Android? How to set the part of the Android TextView as clickable? How to use the Text Watcher class in Android? How to format date and time in android? How to get screen DPI programmatically in Android?

How to get access to the MainActivity findViewById from inside a , onReceive(ChronometerReciever.java:41) at android.app. This broadcast reciever will update a clock, and update a text view in the layout with the updated time. But when I To solve make the broadcast receiver a field in the Activity class. In part 3 of the BroadcastReceiver tutorial, we will send and receive an implicit custom broadcast with an intent action string that we define ourselves.

update ui from background service in android using Broadcast , public class MainActivity extends AppCompatActivity { TextView new BroadcastReceiver() { @Override public void onReceive(Context  에 PushNotificationReceiver (extends BroadcastReceiver)과 MainActivity이 올바르게 작동합니다. 수신자는 Intent을 통해 MainActivity에 일부 데이터 (예 : 문자열 값 "New value")를 보냅니다. 그런 다음 MainActivity은 TextView을이 값으로 업데이트합니다. TextView

Comments
  • Just take one static string and then store this textview's result in it and access it in this class.
  • Thanks for the reply InnocentKiller. Sorry, I'm not sure what you mean. Can you please explain? I appreciate the help.
  • Declare one static string variable inside your MainActivity and then store the value of your textview inside that string and then you can use that string anywhere in any class.
  • I think you can do this, but i am not sure but just give a try.
  • Thanks, I could use a static string however, that wont change the text within the textview in mainactivity. I need to be able to change the text form onreceive().
  • getInstace() does not work in broadcastreceiver: MainActivity .getInstace().updateTheTextView("String"); Doesnt know getinstance
  • just in MainActivity create this method and i also updated the answer.
  • You, InnocentKiller and ashish, are awesome! Works perfect. Thank you a lot.
  • mark the answer solve so that it helps the other to find the answer.
  • private static MainActivity ins; terrible idea which causing activity leaks