为什么没有收到我的本地广播服务? [英] Why isn't my local broadcast from a service being received?

查看:106
本文介绍了为什么没有收到我的本地广播服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含服务(WifiDirectFileTransferService)和广播接收器(WifiDirectBroadcastReceiver)的应用程序.广播接收器用于接收全局广播(对于WifiP2PManager.WIFI_P2P_..._ACTION).接收全球广播的这一部分效果很好.

I have an app that includes a service (WifiDirectFileTransferService) and a broadcast receiver (WifiDirectBroadcastReceiver). The broadcast receiver is used to receive global broadcasts (for WifiP2PManager.WIFI_P2P_..._ACTION). This part, receiving the global broadcasts, works fine.

然后,我尝试为RECEIVED_MESSAGE_DONE_ACTION设置相同的广播接收器,以也接收本地广播.在我的活动中已经为全球广播注册接收器的地方,我添加了本地注册:

Then I tried setting up the same broadcast receiver to also receive a local broadcast, for RECEIVED_MESSAGE_DONE_ACTION. Where I was already registering the receiver for global broadcasts in my activity, I added local registration:

@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);
}

日志消息确认广播接收机在广播发生之前已经注册,并且尚未注销.

The log messages confirm that the broadcast receiver is getting registered, and is not unregistered, before the broadcast occurs.

本地意图过滤器在onCreate()中这样初始化:

The local intent filter is initialized like this, in onCreate():

mLocalIntentFilter.addAction(WifiDirectBroadcastReceiver.RECEIVED_MESSAGE_DONE_ACTION);

本地广播是在onHandleIntent()期间由意图服务发送的:

The local broadcast is sent by the intent service during 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() 从未为此广播被呼叫. 为什么不呢?

but the BroadcastReceiver's onReceive() never gets called for this broadcast. Why not?

是因为广播是从服务发送的吗?没关系...本地广播应该在应用程序中起作用,而服务在应用程序中. (并且此评论确认服务可以发送本地广播到同一应用内的活动).

Is it because the broadcast is being sent from a Service? That shouldn't matter... local broadcasts are supposed to work within the app, and the service is within the app. (And this comment confirms that a service can send a local broadcast to an activity within the same app.)

是因为同一广播接收器用于本地和全局广播吗?就我所知,这也不重要.实际上,我为本地广播尝试了带有单独的BroadcastReceiver的备用版本.行为保持不变:未收到本地广播.

Is it because the same broadcast receiver is being used for both local and global broadcasts? That shouldn't matter either, as far as I can see. In fact, I tried an alternate version with a separate BroadcastReceiver for the local broadcast. The behavior remained the same: the local broadcast didn't appear to be received.

此问题不是没有收到来自服务的本地广播的重复通过活动,其中的问题是本地广播接收器未使用LocalBroadcastManager注册.

This question is not a duplicate of Local broadcast from Service not received by Activity, in which the problem was that the local broadcast receiver wasn't registered using LocalBroadcastManager.

更新:可以肯定的是,我现在使用getApplicationContext()作为LocalBroadcastManager.getInstance()的参数,以防万一,是否在一种情况下传递活动上下文,而在另一种情况下传递应用程序上下文则很重要.这不能解决问题.

Update: Just to be sure, I'm now using getApplicationContext() as the argument to LocalBroadcastManager.getInstance(), in case it matters whether you pass an activity context in one case and an application context in another. This didn't fix the problem.

应David的要求,我要张贴清单的一部分.

At David's request, I'm posting parts of the manifest.

<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" />

如果您还想看其他部分,请告诉我.

If there are other parts you'd like to see, please let me know.

推荐答案

找到了!谈论大海捞针:-)

Found it! Talk about needle in a haystack :-)

您写道:

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作为参数传递!

So you construct and log the broadcast Intent in variable broadcastIntent, but you call sendBroadcast() and pass the variable intent as an argument!

这篇关于为什么没有收到我的本地广播服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆