【问题标题】:RequestLocationUpdate with Pending Intent and how to get the Location Provider(GPS, Network) status?RequestLocationUpdates 与 Pendingintent 以及如何获取位置提供者(GPS、网络)状态?
【发布时间】:2014-12-29 13:36:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: android gps locationmanager location-provider


    【解决方案1】:

    你为什么不使用你的 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);
    

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多