【发布时间】:2011-06-01 02:40:04
【问题描述】:
我只是在尝试这个小示例项目,它所做的一切: 活动一有一个发送广播的按钮。活动二在收到时会显示祝酒词。 下面是代码,广播永远不会收到。我做错了什么?
发送广播
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendBroadcast(View v){
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
sendBroadcast(broadcast);
}
}
收到
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
}
};
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
清单
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SendBroadcast" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"></action>
</intent-filter>
</activity>
</application>
【问题讨论】:
-
那么你找到答案了吗?我有同样的问题。我实际上有一个本地服务,它广播当前打开的活动监听的意图(注册一个广播接收器,就像你在上面做的那样)。活动永远不会收到消息。我没有在清单中放任何东西,但由于我正在注册广播接收器,我认为我不需要。你有没有让你的工作?
-
谢谢。原来我的issue 是另一个涉及指定 MIME 类型的。
-
嗨 nheid 我也在寻找相同的 .. 并且还实现了你上面的代码,但它不起作用意味着 Broadcat 正在发送到其他活动..请帮助..
-
Broadcat 没有发送到其他活动
-
嗨 Garret Wilson,您找到上述解决方案了吗?