Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Intent from BroadcastReceiver class to currently running activity

I have a class which extends BroadcastReceiver. On receiving a SMS, I would like to pass information to my main activity class to display the text in a box (Append, if already text is present).

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
    {
        Intent i = new Intent(context, MainActivity.class);
            i.putExtra("updatedString","Hello");
            context.startActivity(i);
    }
}

MainActivity.java

public class MainActivity extends Activity{

  private TextView results;
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            results = (TextView) findViewById(R.id.results);
            results.setVisibility(View.VISIBLE);
            results.append(extras.getString("updatedString"));
        }

}

I have only one activity class (MainActivity.java). However When i do this I get an exception Unable to pause Activity.

like image 427
user1692342 Avatar asked Jun 14 '15 05:06

user1692342


People also ask

How do I send data from BroadcastReceiver to activity?

startActivity(i); Then, in your activity, you will need to getExtra as so: Intent intent = getIntent(); String message = intent. getStringExtra("message");

Which intent is used by BroadcastReceiver?

Creating a BroadcastReceiverThe intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context. startService(myService); respectively.

Which method do we need to invoke when extending a class with BroadcastReceiver?

registerReceiver() method. The implementing class for a receiver extends the BroadcastReceiver class. If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.


2 Answers

You have three ways:
1) You can define your broadcast inside your MainActivity like this:
in onCreate()

registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));  

and define smsReciver in MainActivity

private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //you can update textBox here
        handler.postDelayed(sendUpdatesToUI, 10);  
    }
};  

define a runnable to update UI

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        update();
    }
};

and update method

private void update(String text) {
    textView.setText(textView.getText().toString() + text);
} 

2) Register a receiver between your Activity and BroadCastReceiver

3) Start your Activity with new Intent to update current open Activity

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("Key", "text");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);  

UPDATE :
explain method 2
MainActivity.class

in onResume()

registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION));  

in onDestroy()

unregisterReceiver(broadcastReceiver);

local broadCast (broadcastReceiver, in MainActivity.class)

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI(intent);
    }
};
private void updateUI(Intent intent) {
    String text = intent.getStringExtra("key");
    textView.setText(textView.getText().toString() + text);
}

SmsReceiver.class
global attribute

public static final String BROADCAST_ACTION = "your.package.name.displayevent";
private final Handler handler = new Handler();
Intent intent;
Context context;

in onReceive()

handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);

this.context = context;//you can retrieve context from onReceive argument

this.intent = new Intent(BROADCAST_ACTION);

define two method

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        display();
    }
};

private void display() {
    intent.putExtra("key", text);
    context.sendBroadcast(intent);
}
like image 149
MHP Avatar answered Oct 20 '22 06:10

MHP


Modify your code as below.

 public class SmsReceiver extends BroadcastReceiver {
@Override 

public void onReceive(Context context, Intent intent)
    { 
        Intent i = new Intent(context, MainActivity.class);
            i.putExtra("updatedString","Hello");            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(i);
    } 
}

public class MainActivity extends Activity{

  private TextView results;
  @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            results = (TextView) findViewById(R.id.results);
            results.setVisibility(View.VISIBLE);
            results.append(extras.getString("updatedString"));
        } 

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
      //handle your intent here.Note this will be called even when activity first created.so becareful to handle intents correctly.
    }

}
like image 21
siva Avatar answered Oct 20 '22 05:10

siva