Android - 无法在我的 activity 中从服务接收本地广播

Android - Unable to receive local broadcast in my activity from service

我的主要 activity 启动了一项服务(位置服务),我希望该服务在每次找到新位置时广播新位置。

多亏了日志,我知道该服务正在运行并且我每隔几秒左右就有一个新位置,但我从未收到广播。

MainActivity.java

public class MainActivity extends Activity {


private static final String TAG = "mainActivity";

private CMBroadcastReceiver mMessageReceiver = new CMBroadcastReceiver();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    // Start Service
    startService(new Intent(this, LocationService.class));

    super.onCreate(savedInstanceState);
}

@Override
public void onResume()
{
    LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE));
    super.onResume();
}

@Override
public void onPause()
{
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    super.onPause();
}
}

CMBroadcastReceiver.java

public class CMBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CMBroadcastReceiver";

public static final String RECEIVE_LOCATION_UPDATE = "LOCATION_UPDATES";

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "Received broadcast");
    String action = intent.getAction();
    if (action.equals(RECEIVE_LOCATION_UPDATE))
    {
        Log.i(TAG, "Received location update from service!");
    }
}
}

LocationService.java

/**
 * Callback that fires when the location changes.
 */
@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    Log.i(TAG, "onLocationChanged " + location);

    Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    Log.i(TAG, "Broadcast sent");
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyclemapapp.gpstracker">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar">
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".LocationService" android:process=":location_service" />
</application>

我的日志我可以看到 "Broadcast Sent" 但我从来没有得到 "Broadcast Received"

如有任何帮助,我们将不胜感激。

编辑:

按照 Shaishav 的建议编辑了在位置服务中创建意图的方式。

还是不行。

在您的 LocationService 中,使用以下方式发送本地广播:

Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
<service android:name=".LocationService" android:process=":location_service" />

您的服务与 activity 处于不同的进程中。 LocalBroadcastManager 只能在一个进程中使用。从 <service> 中删除 android:process,或者使用一些 IPC 机制(例如,系统广播,适当保护)。

LocalBroadcastManager 不能跨进程工作。您的 Service 运行 在一个单独的进程中。

您可以在与 Activity 相同的过程中 运行 您的 Service - 通过从 <service> 元素中删除 process 属性 - 或者改用某种 IPC - 例如,通过在 Context 而不是 LocalBroadcastManager.

上发送和接收广播