Lau*_*Lau 10 provider gps android return
我有这个代码
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVIDER);
Run Code Online (Sandbox Code Playgroud)
即使启用GPS,它也始终返回false.GPS正常工作,但我使用此布尔值显示弹出"没有启用GPS".在这种情况下,弹出窗口每次都会出现
感谢帮助.
Muh*_*aat 25
如果您正在检查位置是否打开或不使用GPS,那么您必须注意以下情况,因为在我的情况下,在设备位置设置中,定位方法设置为BatterySaving模式,其中设备仅使用WiFi移动网络来估算位置:
因此,GPS甚至不用于更新位置,因此位置图标不会出现在状态栏中,除了位置提供者将被称为网络而不是GPS.
因此,要解决该问题,您必须检查提供程序是否包含gps或包含network:
private boolean checkIfLocationOpened() {
String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps") || provider.contains("network"))
return true;
}
// otherwise return false
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果你想用以下方法做到LocationManager:
private boolean checkIfLocationOpened() {
final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
return true;
}
// otherwise return false
return false;
}
Run Code Online (Sandbox Code Playgroud)
试试这种方式..
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
final Intent poke = new Intent();
poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8002 次 |
| 最近记录: |