Muh*_*lib 7 android android-location
我们知道我们有两个替代方案可以让我们的应用程序位置知道我们使用Google的位置服务API(Google Play服务的一部分),或者我们使用Androids Location API.
我在这里遇到的问题是,使用Google的服务,我们必须使用com.google.android.gms.location.LocationListener接口为我们提供位置更新.然而,与android.location.LocationListener不同,谷歌的版本并没有'为我们提供位置提供者的状态(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
Run Code Online (Sandbox Code Playgroud)
.
但是在谷歌我们只有一个
abstract void onLocationChanged(Location location)
Run Code Online (Sandbox Code Playgroud)
如果我使用谷歌服务,我如何识别提供商的变化?.例如,在应用程序使用中,用户决定关闭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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2667 次 |
| 最近记录: |