我的应用包含服务(WifiDirectFileTransferService
)和广播接收器(WifiDirectBroadcastReceiver
)。广播接收器用于接收全局广播(用于WifiP2PManager.WIFI_P2P_..._ACTION
)。接收全球广播的这部分工作正常。
然后我尝试设置相同的广播接收器以接收RECEIVED_MESSAGE_DONE_ACTION
的本地广播。在我的活动中,我已经在全球广播中注册了接收器,我添加了本地注册:
@Override
protected void onResume() {
super.onResume();
// Register the broadcast receiver with the intent values to be matched.
registerReceiver(mReceiver, mIntentFilter);
// Register for local broadcasts too.
Log.i(TAG, "onResume: registering local broadcast receiver " + mLocalIntentFilter.getAction(0));
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(mReceiver, mLocalIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the broadcast receiver.
unregisterReceiver(mReceiver);
Log.i(TAG, "onPause: unregistering local broadcast receiver " + mLocalIntentFilter.getAction(0));
LocalBroadcastManager.getInstance(getApplicationContext())
.unregisterReceiver(mReceiver);
}
在广播发生之前,日志消息确认广播接收者正在注册,并且未注册。
本地意图过滤器在onCreate()
:
mLocalIntentFilter.addAction(WifiDirectBroadcastReceiver.RECEIVED_MESSAGE_DONE_ACTION);
本地广播由意向服务在onHandleIntent()
:
Intent broadcastIntent = new Intent(RECEIVED_MESSAGE_DONE_ACTION)
.putExtra(EXTRA_OTHER_DEVICE_ADDRESS, senderAddress.getHostAddress());
Log.i(TAG, "receiveMessage(): broadcasting " + broadcastIntent.toString());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
但是BroadcastReceiver的onReceive()
永远不会被调用此广播。
为什么不呢?
是因为广播是从服务发送的吗?这不重要......本地广播应该在应用程序内工作,服务在应用程序内。 (this comment确认服务可以向同一个应用中的活动发送本地广播。)
是否因为本地和全球广播使用相同的广播接收器?就我所知,这也不重要。实际上,我尝试了一个备用版本,其中有一个单独的BroadcastReceiver用于本地广播。行为保持不变:当地广播似乎没有收到。
此问题与Local broadcast from Service not received by Activity不重复,其中问题是本地广播接收器未使用LocalBroadcastManager
进行注册。
更新:为了确定,我现在使用getApplicationContext()
作为LocalBroadcastManager.getInstance()
的参数,以防您在一个案例和一个应用程序上下文中传递活动上下文是否重要另一个。这并没有解决问题。
根据大卫的要求,我发布了部分清单。
<uses-permission android:name="android.permission.INTERNET" />
<!-- For Wi-Fi direct (https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html#create-group) -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Do we need this for WiFi Direct? -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
...
<!-- The activity that registers the local broadcast. -->
<activity android:name=".activity.ShareActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<!-- This is the service that sends the local broadcast. -->
<service android:name=".comm.WifiDirectFileTransferService" />
如果还有其他您想看的部分,请告诉我。
答案 0 :(得分:2)
您写道:
Intent broadcastIntent = new Intent(RECEIVED_MESSAGE_DONE_ACTION)
.putExtra(EXTRA_OTHER_DEVICE_ADDRESS, senderAddress.getHostAddress());
Log.i(TAG, "receiveMessage(): broadcasting " + broadcastIntent.toString());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
所以你在变量Intent
中构建并记录广播broadcastIntent
,但是你调用sendBroadcast()
并传递变量intent
作为参数!