【问题标题】:Google Location Services Vs Android Location Services谷歌定位服务与安卓定位服务
【发布时间】:2014-09-29 04:29:51
【问题描述】:

据我们所知,我们有两个替代方案可以让我们的应用程序知道位置 我们要么使用 Google 的 Location Services API(Google Play 服务的一部分),要么使用 Androids Location API。

我在这里遇到的问题是,使用 Google 的服务,我们必须使用接口 com.google.android.gms.location.LocationListener 为我们提供位置更新。 然而,与 android.location.LocationListener 不同的是,google 的版本并没有向我们提供位置提供者(gps 或互联网)的状态。

我们在 Androids Location Listener 中有以下方法

abstract void   onLocationChanged(Location location)
Called when the location has changed.
abstract void   onProviderDisabled(String provider)
Called when the provider is disabled by the user.
abstract void   onProviderEnabled(String provider)
Called when the provider is enabled by the user.
abstract void   onStatusChanged(String provider, int status, Bundle extras)
Called when the provider status changes

.

但是在谷歌我们只有一个

abstract void   onLocationChanged(Location location)

如果我使用谷歌服务,我如何识别提供者的变化? .例如,在应用程序使用过程中,用户决定关闭 GPS,我想在此事件上举杯。

我在黑暗中应该如何实现这一点。

【问题讨论】:

标签: android android-location


【解决方案1】:

您注册一个接收器来处理 Providers_changed 操作。

private static final String ACTION_GPS = "android.location.PROVIDERS_CHANGED";
private BroadcastReceiver yourReceiver;

private void checkGPS() {
    Context context = this.getApplicationContext();
    if (context != null) {
        Button btnGPS = (Button) findViewById(R.id.btnGPS);
        if (btnGPS != null) {
            LocationManager locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            boolean b = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            locationManager = null;
            if (b) {
                if (btnGPS.getVisibility() != View.GONE) {
                    btnGPS.setVisibility(View.GONE);
                }
            } else {
                if (btnGPS.getVisibility() != View.VISIBLE) {
                    btnGPS.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

private void registerReceiverGPS() {
    if (yourReceiver == null) {
        // INTENT FILTER FOR GPS MONITORING
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION_GPS);
        yourReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                    String s = intent.getAction();
                    if (s != null) {
                        if (s.equals(ACTION_GPS)) {
                            checkGPS();
                        }
                    }
                }
            }
        };
        registerReceiver(yourReceiver, theFilter);
    }
}


 @Override
 public void onResume() {
 super.onResume();
 checkGPS();
 registerReceiverGPS();



    @Override
public void onStop() {
    super.onStop();
    // Do not forget to unregister the receiver!!!
    if (yourReceiver != null) {
        unregisterReceiver(yourReceiver);
        yourReceiver = null;
    }

【讨论】:

    【解决方案2】:

    谷歌的版本没有'向我们提供位置提供者的状态(gps 或互联网)

    部分原因是它没有使用任何单一的位置提供程序。

    如果我使用谷歌服务,如何识别提供者的变化?

    您可以尝试收听PROVIDERS_CHANGED_ACTION broadcasts

    【讨论】:

    • 那么您能否建议任何机制,通过它我可以通知用户他忘记从设置中打开他的位置传感器? .我不认为这是太多的要求。这可能会发生很多次。用户将等待位置更新,但不知道他忘记打开适配器这一事实。
    • @MuhammadAhmedAbuTalib:“那么您能否建议任何机制,通过它我可以通知用户他忘记从设置中打开他的位置传感器?” -- 使用LocationManagerisProviderEnabled()。仅仅因为您通过LocationClient 获取您的位置并不意味着您无法使用LocationManager 进行其他操作。而且,仅仅因为LocationClient 提供位置并不意味着LocationClient 必须完全复制LocationManager API 的其余部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多