使用HERE Android SDK Premium,在使用NavigationManager#simulate()方法时,如果添加NavigationManager.GpsSignalListener,即使在模拟器上运行时打开/关闭GPS,也不会调用侦听器回调。
NavigationManager在模拟模式下运行时,如何接收GPS信号事件?
小智 3
可以通过 LocationManager 监听 GPS 的打开/关闭,了解 GPS/网络提供商的状态(启用/禁用)。这与 HERE Android SDK 的 GpsSignalListener 不同,后者用于 GPS 信号丢失(例如在隧道中时)和恢复(即当您退出隧道时)事件。
因此,要监听手机/Android模拟器的GPS开关,您需要订阅Android系统的广播消息并检查LocationManager.PROVIDERS_CHANGED_ACTION. 请参阅活动中的示例代码片段:
@Override
protected void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
this.registerReceiver(gpsOnOffReceiver, iFilter);
}
private BroadcastReceiver gpsOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gpsOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkOn = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsOn || networkOn) {
// GPS is ON
Log.d("GPS", "GPS turned ON");
} else {
// GPS is OFF
Log.d("GPS", "GPS turned OFF");
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |