public static boolean canGetLocation() {
return isLocationEnabled(App.appInstance); // application context
}
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return !TextUtils.isEmpty(locationProviders);
}
}
Run Code Online (Sandbox Code Playgroud)
这是检查设备位置是否启用的快速工具,希望这会有所帮助。
这将返回一个指示启用与否的布尔值。
并且您可以通过使用警报对话框单击侦听器中的以下意图来导航而不是位置设置,
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6845 次 |
最近记录: |