android 定位provider,android-从未调用过位置服务onProviderEnabled

博客内容涉及一个Android应用程序在GPS启用时未触发onProviderEnabled()回调的问题。开发者启动服务请求位置更新,即使在应用程序运行时开启GPS,服务也不会接收到位置提供者启用的通知,除非应用程序重启。问题集中在LocationManager的使用和服务生命周期上。
摘要由CSDN通过智能技术生成

我有一项服务可以更新我的应用中的位置.当我在禁用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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值