Android BroadcastReceiver onReceive Update TextView in MainActivity

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.

  • Google Developer Console: How to see all ratings in review list?
  • Android App Not Install. An existing package by the same name with a conflicting signature is already installed
  • Bottomsheet with moving Floating action buttons
  • Android animation XML issues
  • How to access localhost from a Genymotion android emulator?
  • Concurrent writing to android database (from multiple services)?
  • 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.

    Related posts:

    Can I embed a custom font in an Android application?
    SSLSocket via another SSLSocket
    Develop an Android App to support English and Arabic “layout alignment”
    How to start new activity on button click
    How can I use selenium or appium to automate chrome browser on android?
    Android subscription and Google API
  • Tracing the Memory/Window Leaks in Android?
  • How do i run the Android command line tools?
  • Find missing onError in the project
  • Converting bitmap to byteArray android
  • Android share intent chooser
  • Facebook login with Android SDK 3.0 causing ANRs or not working at all
  • 2 Solutions collect form web for “Android BroadcastReceiver onReceive Update TextView in MainActivity”

    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) {
    
            }           
        }
    }
    

    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”

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.