如何发送和接收广播消息

发布于 2024-09-27 03:38:55 字数 1116 浏览 7 评论 0原文

我正在尝试在选项卡内的两个活动之间传递数据。我正在尝试使用sendBroadcast()。设置断点后,我永远不会到达onReceive()

清单:

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

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

活动发送者:

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

活动接收者:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生不灭 2024-10-04 03:38:55

我遇到了和你一样的问题,但我发现:

从清单中删除意图过滤器并更改

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

希望

Intent intent=new Intent();

它有帮助!

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!

热风软妹 2024-10-04 03:38:55

请使用

intent.getStringExtra("");

new Intent();

为我工作。

Please use

intent.getStringExtra("");

and

new Intent();

Worked for me.

Saygoodbye 2024-10-04 03:38:55

你可以这样做

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

然后为了接收写这样的东西(在活动内)

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

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);
}
握住你手 2024-10-04 03:38:55

发件人活动:

您好,从您的发件人活动中,您只需指定将根据某些条件启动的隐式广播意图的操作和额外数据。

隐式广播的示例;

Intent intent = new Intent();

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

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

sendBroadcast(intent);

您还可以使用 adb:

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

Receiver Avtivity

现在,在接收器的 Activity 上,您通常可以在 onCreate() 或 onResume() 下注册广播。

然后,您将在 onPause() 或 onDestroy() 回调下取消注册,或者如果您销毁了您的活动,这就是您的监听范围仅限于接收器活动生命周期的原因。

注意:从 API26+ 开始,只有当前正在运行的活动(后台或前台)才会检测到这种类型的广播
如果您的目标是 android 8.0(Api26),则无法在应用程序的 Manifest.xml 上声明它们

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文