简体   繁体   English

如何发送和接收广播消息

[英]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() . 我正在尝试使用sendBroadcast() With breakpoints set I never reach onReceive() . 设置断点后,我永远不会达到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);
    }
}

I was having the same problem as you, but I figured out: 我和你有同样的问题,但我发现:

Remove the intent filter from the manifest and change 从清单中删除intent过滤器并进行更改

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

for 对于

Intent intent=new Intent();

Hope it helps! 希望能帮助到你!

Please use 请用

intent.getStringExtra("");

and

new Intent();

Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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