Yack
Yack

Reputation: 1410

How to send and receive broadcast message

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);
    }
}

Upvotes: 61

Views: 120127

Answers (4)

Juan Torres
Juan Torres

Reputation: 77

Sender Activity:

Hello,From your sender's Activity you just need to specify the action and extra data for the implicite broadcast intent that will be launched based on some condition.

Example of an implicite broadcast ;

Intent intent = new Intent();

intent.setAction("com.example.broadcast.MY_NOTIFICATION");

intent.putExtra("data_key", "hello all");

sendBroadcast(intent);

You can also use adb:

adb shell am broadcast -a com.example.broadcast.MY_NOTIFICATION --es data_key "hello all"

Receiver Avtivity:

Now on the receiver's Activity you can normally register your broadcast under your onCreate() or onResume() .

You will then unregister under onPause() or onDestroy() callback or if you destroy your activity, that's why your listening scope is limited to the receiver activity lifetime.

Note: from API26+ , only activities currently running(background or foreground) will detect this type of broadcast You cannot declare them on the Manifest.xml of your application if you target android 8.0(Api26)

Upvotes: 0

M Shafaei N
M Shafaei N

Reputation: 449

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);
}

Upvotes: 2

Paulo Cesar
Paulo Cesar

Reputation: 2268

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!

Upvotes: 42

Abdul
Abdul

Reputation: 89

Please use

intent.getStringExtra("");

and

new Intent();

Worked for me.

Upvotes: 8

Related Questions