我正在运行一项正在获取位置更新的服务。服务成功返回位置。但是之后,我尝试将位置广播给可能正在监听的任何活动。我已经在活动中注册了接收者,但由于某种原因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));
}
我已经尝试过this.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();
}
};
此外,经过研究,我认为某些细节很重要。我之所以没有声明receiver
,是manifest
因为我读到您仅在希望您的应用程序在收到广播时启动时才这样做,而我只希望我的应用程序在已经运行时做出响应。每当服务发送广播时都不会启动。
我还没有延长活动BroadcastReceiver
或者活动以来已经扩展到FragmentActivity
应用程序不会崩溃,并且onLocationChanged位于注册BroadcastReceiver之后正在启动的服务内,因此在广播BroadcastReceiver注册之前不会调用onLocationChanged
我设法解决了这个问题。将来,我会将我的发现和答案发布给其他遇到此问题的人。在这里,我可能已经精简了许多概念,但是为了理解这个特定的问题,我将尽我所能尽力做到准确。
答案:
问题是我没有发送广播,也没有接收广播Context
。我的意思是,这就是我在Manifest
文件中声明服务的方式
<service
android:name=".GoogleFusedLocationApiService"
android:process=":google_fused_location_api_service"/>
该android:process
属性表示此服务将在与应用程序所运行的进程不同的进程上运行。因此,当我调用时,super.registerReceiver(mMessageReceiver, intentFilter);
它是从Activity的上下文中调用的,而当我在broadcaster = LocalBroadcastManager.getInstance(this); broadcaster.sendBroadcast(intent);
此处调用时,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
在第二种情况下,使用是最佳实践和正确的处理方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句