ブロードキャストメッセージの送信と受信方法
-
29-09-2019 - |
質問
タブ内にある2つのアクティビティ間でデータを渡そうとしています。私は使用しようとしています 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);
}
}
解決
私はあなたと同じ問題を抱えていましたが、私は理解しました:
マニフェストから意図フィルターを削除し、変更します
Intent intent=new Intent(getApplicationContext(),WebResults.class);
にとって
Intent intent=new Intent();
それが役に立てば幸い!
他のヒント
使ってください
intent.getStringExtra("");
と
new Intent();
私のために働いた。
所属していません StackOverflow