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.

  • How to add new contacts in android
  • Can Activity.getIntent() ever return null?
  • Get Updated Contacts in Android for Contact Sync
  • Using android support library 23.2 in build.gradle crashes app (XmlPullParserException: XmlPullParserException: invalid drawable tag vector)
  • getActionView() of my MenuItem return null
  • Call Activity method from adapter
  • 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.

  • How to get In-App Product Link under the applications that are listed for your publisher account?
  • What is a “bundle” in an Android application
  • How to determine a wifi channel number used by wifi ap/network?
  • Websockets versus GCM under Android: battery usage?
  • Android sleep() without blocking UI
  • Firebase android : make username unique
  • 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.