繁体   English   中英

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

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

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

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

主活动.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();
}
}

CMB广播接收器.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!");
    }
}
}

定位服务.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>

我在日志中可以看到“已发送广播”但我从未收到“已接收广播”

任何帮助将不胜感激。

编辑:

按照 Shaishav 的建议,编辑了如何在定位服务中创建意图。

还是不行。

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

您可以在与Activity相同的进程中运行您的Service - 通过从<service>元素中删除process属性 - 或者使用某种 IPC 代替 - 例如,通过在Context而不是LocalBroadcastManager上发送和接收广播。

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

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

您的服务与活动处于不同的流程中。 LocalBroadcastManager仅用于一个进程。 要么从<service>删除android:process ,要么使用一些 IPC 机制(例如,系统广播,适当保护)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM