简体   繁体   中英

Passing Data from Broadcast Receiver to another Activity

Hi I've been having an issue with Broadcast Receivers and passing information to another activity. I'm trying to create an application that will capture incoming SMS messages, look for a website in the text, then pop up an Alert Dialog box asking if the user wants to go to the website.

public class TextReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent)
{
    // .. other code that
    // sets received SMS into message

    Toast toast = Toast.makeText(context,
            "Received Text: " + message.getMessageBody(), Toast.LENGTH_LONG);
            toast.show();
 }

So that code works fine, receive a text it pops up a toast with the message. The toast is useless but it shows the receiver works. But I want to communicate with an activity to show an Alert Dialog and start up a webView. I already programmed the code that will take a string search for the website and open the webView. Is it possible to get the string from the broadcast receiver and do something like this?:

    public class ReceiveText extends Activity{
public void onCreate(Bundle savedInstanceState) {

// Somehow pass the string from the receiver into this activity, 
//stored in variable messages

findOpen(messages);

// is that possible?

}
public class findOpen(string messages){
// do stuff ... open alert...open site if OK
}

So basically I just want to pass a string from a Broadcast Receiver to another activity that will use that string. The rest of the code is basically in place all I need is that string... I'm new to this and Java and any help would be much appreciated. Thanks

If you have your activity named ReceiveText , then in your BroadcastReceiver , you should do the following:

Intent i = new Intent(context, ReceiveText.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", message.getMessageBody());
context.startActivity(i);

Then, in your activity, you will need to getExtra as so:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

And then you will use message as you need.

If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.

EDIT: This restarts your activity. this answer is likely a better solution for most people.

  1. Instantiate a BroadcastReceiver in the activity you want to get your data to, for example:

     private BroadcastReceiver mServiceReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { //Extract your data - better to use constants... String IncomingSms=intent.getStringExtra("incomingSms");// String phoneNumber=intent.getStringExtra("incomingPhoneNumber"); } };
  2. Unregister your receiver on onPause() :

     @Override protected void onPause() { super.onPause(); try { if(mServiceReceiver != null){ unregisterReceiver(mServiceReceiver); } } catch (Exception e) { e.printStackTrace(); } }
  3. Register it on onResume() :

     protected void onResume() { super.onResume(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.SmsReceiver"); registerReceiver(mServiceReceiver , filter); }
  4. Broadcast your data from the service via an Intent, for Example:

     Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message); i.putExtra("incomingPhoneNumber", phoneNumber); context.sendBroadcast(i);

and that's it! goodLuck!

We can send the data from onReceive to another activity using LocalBroadcastManager. It means you are again broadcasting the data using the context

@Override
    public void onReceive(Context context, Intent intent) {
    
        Log.d("Broadcast", "wifi  ConnectivityReceiver");
    
                Bundle extras = intent.getExtras();
                Intent intent = new Intent("broadCastName");
                // Data you need to pass to another activity
                intent .putExtra("message", extras.getString(Config.MESSAGE_KEY)); 
                context.sendBroadcast(intent );
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM