位置服务 onProviderEnabled 从未被调用
我有一项可以在我的应用程序中更新位置的服务。当我在禁用 GPS 的情况下启动应用程序,返回 Android 菜单并启用 GPS,最后返回我的应用程序(服务尚未被销毁)时,永远不会调用 onProviderEnabled。有人可以帮忙吗?
更新:如果我重新启动应用程序,则会启用提供程序。仅 onProviderEnabled 未被调用...
在我需要位置的每个活动中,我都会这样做
super.onCreate(savedInstanceState);
//....
// Bind location service
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE);
//....
,并且
@Override
protected void onDestroy() {
super.onDestroy();
// Unbind LocationService
ItemDetail.this.unbindService(mConnection);
}
服务是
public class LocationService extends Service implements LocationListener {
LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
// Update after minimum 5 minutes and if user has moved at least 100 meters.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
Location loc = getBestLocation(locationManager);
if(loc!=null){
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
}
}
public void onLocationChanged(Location loc) {
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
public static Location getBestLocation(LocationManager locationManager) {
Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;
}
public void onProviderEnabled(String s){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
}
public void onProviderDisabled(String s){
locationManager.removeUpdates(this);
GlobalVars.lat = null;
GlobalVars.lng = null;
}
public void onStatusChanged(String s, int i, Bundle b){}
@Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
}
I have a service to update location in my app. When I start my app with GPS disable, go back to android menu and enable GPS, and finally go back to my app (the service has not been destroyed), onProviderEnabled is never called. Anybody could help?
UPDATE: if I restarts the app the provider is enabled. Only onProviderEnabled is not called...
In every activity in which I need location I do
super.onCreate(savedInstanceState);
//....
// Bind location service
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE);
//....
and
@Override
protected void onDestroy() {
super.onDestroy();
// Unbind LocationService
ItemDetail.this.unbindService(mConnection);
}
and the service is
public class LocationService extends Service implements LocationListener {
LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
// Update after minimum 5 minutes and if user has moved at least 100 meters.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
Location loc = getBestLocation(locationManager);
if(loc!=null){
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
}
}
public void onLocationChanged(Location loc) {
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
public static Location getBestLocation(LocationManager locationManager) {
Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;
}
public void onProviderEnabled(String s){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
}
public void onProviderDisabled(String s){
locationManager.removeUpdates(this);
GlobalVars.lat = null;
GlobalVars.lng = null;
}
public void onStatusChanged(String s, int i, Bundle b){}
@Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
LocationService.onCreate()
方法中,您的代码检查 GPS 提供商是否已禁用。但只有在订阅已启用的情况下,您才可以开始订阅。请注意,您的侦听器永远不会向
LocationManager
注册,也不会收到禁用或启用位置提供程序的任何更新。而是将您的onCreate()
方法更改为始终调用In your
LocationService.onCreate()
method your code checks if the GPS provider is disabled.But you only start the subscription if it is already enabled. Note that your listener is then never registered with the
LocationManager
and will not receive any updates of location providers being disabled or enabled. Rather change youronCreate()
method to always call