gpt4 book ai didi

android - 谷歌定位服务与安卓定位服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:30 25 4
gpt4 key购买 nike

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

我在这里遇到的问题是,使用 Google 的服务,我们必须使用为我们提供位置更新的接口(interface) 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)

如果我使用的是 Google 服务,我将如何识别提供商的变化? .例如在应用程序使用过程中,用户决定关闭GPS,我想为这个事件干杯。

我不知道该如何实现。

最佳答案

您注册一个接收器来处理 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 - 谷歌定位服务与安卓定位服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25156977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com