17

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.

user3499199
  • 183
  • 1
  • 1
  • 4
  • Just take one static string and then store this textview's result in it and access it in this class. – InnocentKiller Apr 04 '14 at 18:03
  • Thanks for the reply InnocentKiller. Sorry, I'm not sure what you mean. Can you please explain? I appreciate the help. – user3499199 Apr 04 '14 at 18:11
  • 1
    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. – InnocentKiller Apr 04 '14 at 18:14
  • I think you can do this, but i am not sure but just give a try. – InnocentKiller Apr 04 '14 at 18:18
  • 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(). – user3499199 Apr 04 '14 at 18:20
  • You can use Handler class, It is more natural way to update UI from non UI Thread. http://stackoverflow.com/questions/14643385/how-to-update-ui-in-a-broadcastreceiver – Faisal Ali Apr 04 '14 at 18:25

5 Answers5

36

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

        }           
    }
}
ssi-anik
  • 2,998
  • 3
  • 23
  • 52
User10001
  • 1,295
  • 2
  • 18
  • 30
  • getInstace() does not work in broadcastreceiver: MainActivity .getInstace().updateTheTextView("String"); Doesnt know getinstance – user3499199 Apr 04 '14 at 18:22
  • just in MainActivity create this method and i also updated the answer. – User10001 Apr 04 '14 at 18:26
  • You, InnocentKiller and ashish, are awesome! Works perfect. Thank you a lot. – user3499199 Apr 04 '14 at 18:33
  • mark the answer solve so that it helps the other to find the answer. – User10001 Apr 04 '14 at 18:35
  • Thank you both. Will mark solved. Only one problem. Error generated when MainActivity is now running. Works perfect when class is running but errors when not running. Also when MainActivity is running getinstance recreates mainactivity so now 2 are running. – user3499199 Apr 04 '14 at 18:54
  • getinstance method does not creates the Instance of MainActivity it only give the reference of current instance of the MainActivity. – User10001 Apr 04 '14 at 19:04
  • Worked for me. Thanks! – Manoj Behera Jun 26 '15 at 12:01
  • Love this! Thank you. – Dayan Aug 27 '15 at 12:09
  • @User10001 But if I have `ins = null` in public static ReceiverActivity `getInstance() { Log.d("RECEIVER_ACTIVITY", "getInstance => "+String.valueOf(ins)); return ins; }` what i should do with it? – vladimir Sep 13 '16 at 10:04
  • 6
    `private static MainActivity ins;` terrible idea which causing activity leaks – Selvin Jan 20 '17 at 14:11
5

#Use Interface Another way to deal with this situation is by using an Interface. I will describe the advantage of this approach but first, let's see how it's done.

Follow these steps:
1) Create an interface

public interface MyBroadcastListener{

    public void doSomething(String result);

}

2) Initialize the listener in BroadCastReceiver

public class NotifAlarm extends BroadcastReceiver {

    private MyBroadcastListener listener;

    @Override
    public void onReceive(Context context, Intent intent) {
       
        listener = (MyBroadcastListener)context;

        // other things done here like notification

        // NUPDATE TEXTV1 IN MAINACTIVITY HERE
        listener.doSomething("Some Result");
    }
}

3) Implement the interface in Activity and override the method

public YourActivity extends AppCompatActivity implements MyBroadcastListener{

    // Your Activity code 

    public void updateTheTextView(String t) {
        TextView textV1 = (TextView) findViewById(R.id.textV1);
        textV1.setText(t);
    }

    @Override
    public void doSomething(String result){
         updateTheTextView(result);          // Calling method from Interface
    }

 }

##Advantages of using the interface?

  • When you have BroadcastReceiver in a different file
  • Decoupled BroadcastReceiver

Using an interface makes BroadcastReceiver independent of any Activity. Let's say in future you want to use this BroadCastReceiver with another Activity which takes the result from BroadcastReceiver and start a DetailActivity. This is completely a different task but you will use the same BroadcastReceiver without even a single code change inside BroadcastReceiver.

How to do that?
Implement the interface in the Activity and Override the method. That's it!

public ListActivity extends AppCompatActivity implements MyBroadcastListener{

    // Your Activity code 

    public void startDetailActivity(String title) {
        Intent i = new Intent(ListActivity,this, DetailActivity.class);
        i.putExtra("Title", title);
        startActivity(i);
    }

    @Override
    public void doSomething(String result){
         startDetailActivity(String title);    // Calling method from Interface
    }

 }
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
2

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"

ShahinFasihi
  • 624
  • 7
  • 13
2

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

        }
    }
}
Santhosh
  • 81
  • 2
  • 11
Renato Aloi
  • 300
  • 2
  • 9
  • Not working in the updateTheTextView function: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference- This is the issue that I keep facing. – Brian M Aug 14 '20 at 20:44
0

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);
  }
}
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33