谷歌位置服务和Android手机定位服务 [英] Google Location Services Vs Android Location Services

查看:257
本文介绍了谷歌位置服务和Android手机定位服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们知道我们有两名候补使我们的应用程序了位置感知 我们要么使用谷歌的位置服务API(谷歌的一部分Play业务),或者我们用机器人定位API。

As we know we have two alternates for making our applicaiton location aware Either we use Google’s Location Services API (part of Google Play services) or we use Androids Location API .

我的问题在这里的是,使用谷歌的服务,我们要使用的界面com.google.android.gms.location.LocationListener这为我们提供了位置更新。 然而不同的是android.location.LocationListener,谷歌的版本犯规为我们提供了位置提供商(全球定位系统或互联网)的状态

The problem I have here is that using the Google's services , we have to use interface com.google.android.gms.location.LocationListener that provides us with location updates. However unlike the android.location.LocationListener, the google's version doesnt' provides us the state of the location provider (gps or internet).

我们在机器人位置监听器下面的方法

we have the following methods in the 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

然而,在谷歌,我们只有一个

However in google we have just one

abstract void   onLocationChanged(Location location)

我将如何确定的变化,供应商,如果我使用谷歌的服务? 。例如在应用程序的使用当中,用户决定关闭GPS,我想表现出对本次活动干杯。

How would I identify the changes in the provider if I am using the google services ? . For example in midst of the application usage , the user decides the shut off the GPS , I wanted to show a toast on this event.

我在黑暗中,我应该怎么acheive的。

I am in the dark how should I acheive that .

推荐答案

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

You register a receiver to handle the actiion 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;
    }

这篇关于谷歌位置服务和Android手机定位服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆