Android - Unable to receive local broadcast in my

2020-02-07 06:21发布

问题:

I have my main activity that start a service (Location service) and I want that service to broadcast the new location each time a new location is found.

Thanks to the log I know the service is working and I have new locations every seconds or so, but I never get the broadcast.

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>

I the log I can see that "Broadcast Sent" But I never get the "Broadcast Received"

Any help will would be greatly appreciated.

EDIT:

Edited how the intent was created in the location service as Shaishav suggested.

Still doesn't work.

回答1:

LocalBroadcastManager does not work across processes. Your Service is running in a separate process.

You can either run your Service in the same process as the Activity - by removing the process attribute from the <service> element - or use some sort of IPC instead - e.g., by sending and receiving the broadcasts on a Context instead of LocalBroadcastManager.



回答2:

In your LocationService, send local broadcast using:

Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);


回答3:

<service android:name=".LocationService" android:process=":location_service" />

Your service is in a separate process from the activity. LocalBroadcastManager is only for use in one process. Either remove android:process from the <service>, or use some IPC mechanism (e.g., system broadcasts, properly secured).