如何发送和接收广播消息
我正在尝试在选项卡内的两个活动之间传递数据。我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了和你一样的问题,但我发现:
从清单中删除意图过滤器并更改
希望
它有帮助!
I was having the same problem as you, but I figured out:
Remove the intent filter from the manifest and change
for
Hope it helps!
请使用
并
为我工作。
Please use
and
Worked for me.
你可以这样做
然后为了接收写这样的东西(在活动内)
You can do like this
Then for receiving write something like this (inside Activity)
发件人活动:
您好,从您的发件人活动中,您只需指定将根据某些条件启动的隐式广播意图的操作和额外数据。
隐式广播的示例;
您还可以使用 adb:
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 ;
You can also use adb:
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)