Java BroadcastReceiver onReceive方法未调用?

Java BroadcastReceiver onReceive方法未调用?,java,android,broadcastreceiver,broadcast,android-broadcastreceiver,Java,Android,Broadcastreceiver,Broadcast,Android Broadcastreceiver,我有一个正在运行的服务,其中我正在获取位置更新。服务成功返回位置。但在那之后,我尝试向任何可能正在收听的活动广播该位置。我已在活动中注册了接收器,但由于某种原因,onReceive方法未被调用。 这是我的onLocationChanged方法中的代码 @Override public void onLocationChanged(Location location) { Intent intent = new Intent(); intent.setAction("Locatio

我有一个正在运行的服务,其中我正在获取位置更新。服务成功返回位置。但在那之后,我尝试向任何可能正在收听的活动广播该位置。我已在活动中注册了接收器,但由于某种原因,
onReceive
方法未被调用。
这是我的
onLocationChanged
方法中的代码

@Override
public void onLocationChanged(Location location) {
    Intent intent = new Intent();
    intent.setAction("LocationBroadcast");
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    intent.putExtra("lat", lat);
    intent.putExtra("lng", lng);
    //I am initializing the broadcaster object in onCreate method of my service but I am putting it here for simplicity
    broadcaster = LocalBroadcastManager.getInstance(this);
    //This Toast successfully shows my coordinates so I know the problem is not with this method
    Toast.makeText(GoogleFusedLocationApiService.this, ""+lat+", "+lng+"", Toast.LENGTH_SHORT).show();
    broadcaster.sendBroadcast(intent);
}
在onCreate方法的活动中,我正在注册
LocationBroadcast
,就像这样

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    ...
    IntentFilter intentFilter = new IntentFilter("LocationBroadcast");
    super.registerReceiver(mMessageReceiver, intentFilter);
    startService(new Intent(MyApp.getAppContext(), GoogleFusedLocationApiService.class));
}
我尝试了
这个.registerReceiver(mMessageReceiver,intentFilter)
LocalBroadcastManager.getInstance(this.registerReceiver)(mMessageReceiver,intentFilter)但两者都不起作用

这是我的
mMessageReceiver
定义

public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        double lat = intent.getDoubleExtra("lat", 0);
        double lng = intent.getDoubleExtra("lng", 0);
        // This Toast never shows and neither can I debug this method at all
        // so for now the only conclusion is the broadcast is not being received
        Toast.makeText(MyApp.getAppContext(), "Cordinates are "+lat+", "+lng+"", Toast.LENGTH_SHORT).show();
    }
};
此外,经过一些研究,我可能认为一些细节很重要。我没有在
manifest
中声明
receiver
,因为我了解到只有当您希望应用程序在接收到广播时启动时才会这样做,但我只希望我的应用程序在已经运行时才作出反应。不在服务发送广播时启动

我也没有将活动扩展到
BroadcastReceiver
,因为活动已经扩展到
FragmentActivity


应用程序不会崩溃,onLocationChanged位于注册BroadcastReceiver后启动的服务内,因此在注册BroadcastReceiver之前不会调用onLocationChanged。我已设法解决此问题。我会把我的发现和答案发布给将来面对这个问题的任何人。我可能已经在这里简化了很多概念,但为了理解这个具体问题,我将尽我所能做到准确

答案是:

问题是我没有使用相同的
上下文发送和接收广播。我的意思是,这就是我在
Manifest
文件中声明我的服务的方式

<service
        android:name=".GoogleFusedLocationApiService"
        android:process=":google_fused_location_api_service"/>

android:process
属性意味着该服务将在与应用程序运行的进程不同的进程上运行。所以当我调用时,
super.registerReceiver(mMessageReceiver,intentFilter)它是从活动的上下文调用的,当我调用
broadcaster=LocalBroadcastManager.getInstance时(这是);广播者。发送广播(意图)
此处
sendBroadcast
是从服务的上下文调用的,该上下文为该服务运行了一个不同的进程。你看,我是从一个不同的上下文注册接收器,然后从一个不同的上下文发送广播

LocalBroadcastManager
仅在从同一进程(即上下文)发送和接收广播时起作用。因此,在这种情况下,由于我的服务和我的应用程序/活动正在运行或是分开的进程,即上下文,我不能使用
LocalBroadcastManager
。我需要使用全局广播,并确保我正在注册广播并从同一上下文发送广播

现在,由于我有一个应用程序的静态上下文,只要调用,
MyApp.getAppContext()
就可以在任何地方使用,如果我注册广播接收器并使用此上下文发送广播,您可以从现在开始学习如何做,这意味着两者都是从相同的上下文中完成的,即
MyApp.getAppContext()
现在我开始成功接收广播

总之,如果您的服务有单独的流程,请使用
MyApp.getAppContext().registerReceiver()
MyApp.getAppContext().sendBroadcast()

如果您的服务具有相同的进程,或者清单文件中的服务标记中没有
android:process
属性,那么您可以使用
LocalBroadcastManager.getInstance(this.registerReceiver()
LocalBroadcastManager.getInstance(this.sendBroadcast()


您仍然可以在此处使用
MyApp.getAppContext
,但在第二种情况下,使用
LocalBroadcastManager
是最佳做法和正确的操作方式。

接收器具体位于何处?在与
onlocationChanged
或其他活动相同的活动中,
onlocationChanged
位于单独的服务中。接收器位于活动内部,例如,在广播时将显示该活动。为什么要调用super.registerReceiver?嗯,注册我的BroadcastReceiver以收听我的服务正在发送的特定广播。另一件要检查的事情是,您是否阅读了文档@是否尝试注册LocalBroadcastManager的registerReceiver而不是Activity方法?我相信。registerReceiver和registerReceiver可能不同。由于您正在发送一个本地广播,可能您没有发送到实际的接收者。我很有信心这就是问题所在。如果这是有效的让我知道,我会张贴这作为一个答案。