59

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
  • 3,986
  • 3
  • 31
  • 59
Yack
  • 1,380
  • 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

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,003
  • 5
  • 27
  • 47
Paulo Cesar
  • 2,220
  • 1
  • 25
  • 34
8

Please use

intent.getStringExtra("");

and

new Intent();

Worked for me.

Marvin Pinto
  • 28,922
  • 7
  • 36
  • 53
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
  • 311
  • 2
  • 6