位置服务 onProviderEnabled 从未被调用

发布于 2024-11-16 20:37:11 字数 2924 浏览 0 评论 0原文

我有一项可以在我的应用程序中更新位置的服务。当我在禁用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-11-23 20:37:11

在您的 LocationService.onCreate() 方法中,您的代码检查 GPS 提供商是否已禁用。

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

但只有在订阅已启用的情况下,您才可以开始订阅。请注意,您的侦听器永远不会向 LocationManager 注册,也不会收到禁用或启用位置提供程序的任何更新。而是将您的 onCreate() 方法更改为始终调用

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, 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 disabled or enabled. Rather change your onCreate() method to always call

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文