Android RequestLocationUpdate具有挂起的意图,以及如何获取位置提供程序(GPS、网络)状态?

Android RequestLocationUpdate具有挂起的意图,以及如何获取位置提供程序(GPS、网络)状态?,android,gps,locationmanager,location-provider,Android,Gps,Locationmanager,Location Provider,我已经使用Android SDK位置管理器来获取用户位置 我正在使用待定意图请求位置信息。我使用了pending intent,因为即使应用程序未运行,我也需要发送位置信息 Intent mLocationIntent = new Intent(mContext, LocationIntentService.class); mLocationUpdatePendingIntent = PendingIntent.getService(mContext, 1,

我已经使用Android SDK位置管理器来获取用户位置

我正在使用待定意图请求位置信息。我使用了pending intent,因为即使应用程序未运行,我也需要发送位置信息

Intent mLocationIntent = new Intent(mContext,
            LocationIntentService.class);
mLocationUpdatePendingIntent = PendingIntent.getService(mContext, 1,
            mLocationIntent, 0);
mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0,
                mLocationUpdatePendingIntent);
使用上述代码注册后,我将在LocationIntentService类的OnHandleContent中获得位置信息

但是我没有onProviderEnabled或onProviderDisabled方法来监听gps和网络提供商的状态。而且我也不想使用融合位置api


如何通过使用挂起的意图请求位置更新来实现这两个目标。或以其他方式注册位置信息和位置提供程序状态

为什么不使用locationlistener来代替意图

大概是这样的:

LocationListener mLocationListener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub                  

                }
};
然后,要进行更新,请使用:

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER))    
                    manager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null);

你为什么不用locationlistener来代替意图呢

大概是这样的:

LocationListener mLocationListener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub                  

                }
};
然后,要进行更新,请使用:

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER))    
                    manager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null);