2

我使用 Android SDK Location Manager 来获取用户位置。

我正在使用 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 类的 onHandleIntent 处获取位置信息。

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

如何通过使用 Pending Intent 请求位置更新来实现这两个目标。或者以任何其他方式注册位置信息以及位置提供者状态。

4

1 回答 1

-1

你为什么不使用你的位置监听器而不是意图?

像这样的东西:

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);
于 2016-07-25T15:25:24.057 回答