Android-无法通过服务接收我的活动中的本地广播 [英] Android - Unable to receive local broadcast in my activity from service

查看:72
本文介绍了Android-无法通过服务接收我的活动中的本地广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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"

任何帮助将不胜感激.

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

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

仍然无法正常工作.

推荐答案

LocalBroadcastManager在整个进程中均不起作用.您的Service在单独的进程中运行.

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

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

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.

这篇关于Android-无法通过服务接收我的活动中的本地广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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