如何发送和接收广播消息

如何发送和接收广播消息

本文介绍了如何发送和接收广播消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两项活动是标签内部之间传递数据。我试图用sendBroadcast。断点设置我永远达不到的onReceive。

清单:

 <活动机器人:WebResultsNAME =
              机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
    <作用机器人:名称=com.toxy.LOAD_URL/>
   &所述; /意图滤光器>
  < /活性GT;
 

活动发件人:

 意向意图=新的意图(getApplicationContext(),WebResults.class);
intent.setAction(com.toxy.LOAD_URL);
intent.putExtra(URL,uri.toString());
sendBroadcast(意向);
 

活动Reciever:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    IntentFilter的过滤器=新的IntentFilter(com.toxy.LOAD_URL);
    this.registerReceiver(新的接收器(),过滤器);
}


 私有类接收器扩展的BroadcastReceiver {

 @覆盖
 公共无效的onReceive(背景为arg0,意图ARG1){

    字符串URL = arg1.getExtras()的getString(URL)。
    的WebView的WebView =(web视图)findViewById(R.id.webView);
    webview.loadUrl(URL);
 }

}
 

解决方案

我有同样的问题,因为你,但我想通了:

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

意向意图=新的意图(getApplicationContext(),WebResults.class);

意向意图=新的意图();

希望它能帮助!

I am trying to pass data between two activities that are inside of tabs. I am trying to use the sendBroadcast. With breakpoints set I never reach the 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 Reciever :

@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=new Intent(getApplicationContext(),WebResults.class);

for

Intent intent=new Intent();

Hope it helps!

这篇关于如何发送和接收广播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:24