Android - 无法在我的活动中从服务接收本地广播 [英] Android - Unable to receive local broadcast in my activity from service
问题描述
我的主要活动是启动服务(位置服务),我希望该服务在每次找到新位置时广播新位置.
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>
我在日志中可以看到Broadcast Sent"但我从未收到Broadcast Received"
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
- 通过从 < 中删除
元素 - 或者使用某种 IPC 代替 - 例如,通过在 process
属性.service>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屋!