60

I am trying to pass data between two activities that are inside of tabs. I am trying to use sendBroadcast(). With breakpoints set I never reach onReceive().

Manifest:

<activity
    android:name=".WebResults"
    android:label="@string/app_name">

    <intent-filter>
        <action android:name="com.toxy.LOAD_URL" />
    </intent-filter>         
</activity>

Activity Sender:

Intent intent=new Intent(getApplicationContext(),WebResults.class);
intent.setAction("com.toxy.LOAD_URL");
intent.putExtra("url",uri.toString());
sendBroadcast(intent);

Activity Receiver :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    IntentFilter filter = new IntentFilter("com.toxy.LOAD_URL");
    this.registerReceiver(new Receiver(), filter);
}

private class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        String url = arg1.getExtras().getString("url");
        WebView webview =(WebView)findViewById(R.id.webView);
        webview.loadUrl(url);
    }
}
moritzg
  • 4,266
  • 3
  • 37
  • 62
Yack
  • 1,400
  • 1
  • 10
  • 13
  • Put everything in one activity, rather than using separate activities for the tabs, and you will no longer need to try to use broadcasts to communicate between them. – CommonsWare Oct 11 '10 at 17:17
  • How i can unregister this receiver? – AlexS Sep 17 '19 at 12:58
  • you should change the question title, it is not helping people who searched "How to send and receive broadcast message" and got here by google first result, the people who answered you all suggested another approach other than sending broadcast – Alireza Jamali Feb 04 '23 at 20:53

3 Answers3

43

I was having the same problem as you, but I figured out:

Remove the intent filter from the manifest and change

Intent intent=new Intent(getApplicationContext(),WebResults.class);

for

Intent intent=new Intent();

Hope it helps!

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Paulo Cesar
  • 2,250
  • 1
  • 25
  • 35
8

Please use

intent.getStringExtra("");

and

new Intent();

Worked for me.

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
Abdul
  • 89
  • 1
  • 1
2

You can do like this

Intent intent = new Intent("msg");    //action: "msg"
intent.setPackage(getPackageName());
intent.putExtra("message", message.getBody());
getApplicationContext().sendBroadcast(intent);

Then for receiving write something like this (inside Activity)

@Override
protected void onResume() {
    super.onResume();
    mBroadcastReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent){
           /* Toast.makeText(context, "Message is: "+ intent.getStringExtra("message"), Toast.LENGTH_LONG)
                    .show();*/
            String action = intent.getAction();
            switch (action){
                case "msg":
                    String mess = intent.getStringExtra("message");
                    txt.setText(mess);
                    break;
            }
        }

    };

    IntentFilter filter = new IntentFilter("msg");
    registerReceiver(mBroadcastReceiver,filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mBroadcastReceiver);
}
M Shafaei N
  • 409
  • 3
  • 6