1

I want to update my text view on the receivers side as soon as I receive the message. I have the following code which creates the instance of main activity and uses it in the Broadcast receiver to update UI. But the text view isn't getting updated??

public class Mainactivity extends activity{
private static MainActivity ins;

public static MainActivity getInst()
{
    return ins;
}
protected void onCreate(Bundle savedInstanceState) {
ins=this;}
public void updateUI(final String s)
{
MainActivity.this.runOnUiThread(new Runnable() {
     @Override
     public void run() {
         TextView  textView=(TextView) findViewById(R.id.textView);
         textView.setText(s);
     }
 });
}

In the smsreceiver class

public class smsreceiver extends BroadcastReceiver
{
 try{

             if (MainActivity.getInst()!=null)
                MainActivity.getInst().updateUI(str);

        }
        catch(Exception e){
            e.printStackTrace();
        }
}

Please help me out!!

Chris623
  • 2,464
  • 1
  • 22
  • 30

2 Answers2

0

What if you try like below:

public class Mainactivity extends activity{

    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.your_layout);
        //register your receiver
    }

    protected void onDestroy() {

        //unregister your receiver
        super.onDestroy();
    }
    public void updateUI(final String s)
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView  textView=(TextView) findViewById(R.id.textView);
                textView.setText(s);
            }
       });
    }

    private class smsreceiver extends BroadcastReceiver
    {
        try{

             updateUI(str)

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
0

this is my code base you code it can work

public class MainActivity extends Activity {

private static MainActivity ins;

private Button mButton;

public static MainActivity getInst()
{
    return ins;
}

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

    mButton = (Button) findViewById(R.id.but_send);

    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent inten = new Intent(MainActivity.this, SmsReceiver.class);
            inten.setAction("com.example.demo.action.info");
            sendBroadcast(inten);
        }
    });
}

public void updateUI(final String s)
{
    MainActivity.this.runOnUiThread(new Runnable() {
     @Override
     public void run() {
         TextView  textView=(TextView) findViewById(R.id.tv_info);
         textView.setText(s);
     }
 });
}

}

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    try {
        if (MainActivity.getInst() != null)
            MainActivity.getInst().updateUI("Hello World");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

    <receiver android:name="com.example.demoexample.SmsReceiver" >
        <intent-filter>
            <action android:name="com.example.demo.action.info" />
        </intent-filter>
    </receiver>
zhang
  • 41
  • 2