位置服务onProviderEnabled从来没有所谓的 [英] Location service onProviderEnabled never called

查看:183
本文介绍了位置服务onProviderEnabled从来没有所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在我的应用程序更新的位置服务。当我开始我的带GPS关闭应用程序,返回到Android菜单,启用GPS,终于回到我的应用程序(该服务并没有被破坏),onProviderEnabled永远不会被调用。任何人都可以帮助?

更新:如果我重新启动应用程序的供应商已启用。只有onProviderEnabled不叫...

在每一项活动中,我需要的位置怎么办

  super.onCreate(savedInstanceState);

    // ....

    //绑定位置服务
    bindService(新意图(这一点,LocationService.class),mConnection,Context.BIND_AUTO_CREATE);

    // ....
 

  @覆盖
保护无效的onDestroy(){
    super.onDestroy();
    //取消绑定LocationService
    ItemDetail.this.unbindService(mConnection);
}
 

和服务是

 公共类LocationService扩展服务实现LocationListener的{
    LocationManager locationManager;

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    @覆盖
    公共无效的onCreate(){
        locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

        如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

            //至少5分钟后用户和如果更新已经移动了至少100米。
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5 * 60 * 1000,100,这一点);

            位置LOC = getBestLocation(locationManager);
            如果(LOC!= NULL){
                GlobalVars.lat =(双人间)(loc.getLatitude());
                GlobalVars.lng =(双人间)(loc.getLongitude());
            }
        }
    }

    公共无效onLocationChanged(位置LOC){
        GlobalVars.lat =(双人间)(loc.getLatitude());
        GlobalVars.lng =(双人间)(loc.getLongitude());
    }

    公共静态位置getBestLocation(LocationManager locationManager){

        位置location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        位置location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        //如果两者都可用,获取最新的
        如果(location_gps = NULL和放大器;!&安培;!location_network = NULL){
            返程(location_gps.getTime()> location_network.getTime())location_gps:location_network;
        }
        否则,如果(location_gps == NULL和放大器;&安培; location_network == NULL){
            返回null;
        }
        其他
            返程(location_gps == NULL)location_network:location_gps;?

    }

    公共无效onProviderEnabled(String s)将{
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5 * 60 * 1000,100,这一点);
    }
    公共无效onProviderDisabled(String s)将{
        locationManager.removeUpdates(本);
        GlobalVars.lat = NULL;
        GlobalVars.lng = NULL;
    }
    公共无效onStatusChanged(字符串S,INT I,叠B){}

    @覆盖
    公共无效的onDestroy(){
        locationManager.removeUpdates(本);
    }
}
 

解决方案

在你的 LocationService.onCreate()方法的code盘,如果GPS提供商被禁止

如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

但是,你才开始订阅,如果已经启用。需要注意的是你的听众,然后永远与LocationManager注册,将不会收到位置提供的任何更新被disalbed或启用。而是改变你的onCreate方法总是调用

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5 * 60 * 1000,100,这一点);

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);
    }
}

解决方案

In your LocationService.onCreate() method your code checks if the GPS provider is disabled.

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

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 disalbed or enabled. Rather change your onCreate method to always call

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);

这篇关于位置服务onProviderEnabled从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆