【问题标题】:to check location service enable or not in android device在 android 设备中检查位置服务是否启用
【发布时间】:2021-12-28 11:51:24
【问题描述】:
很抱歉问这个问题,但我不是 Android 应用程序的开发人员。我想问是否可以对自定义地理定位 android 应用程序进行编码,这样当用户启动应用程序并检测到设备的位置服务已关闭时,它将显示为提示,或者应用程序在定位服务之前不会继续进行是否由用户手动开启?
我们正在使用移动设备管理 (mdm) 来管理 android 移动设备,但 mdm 没有强制执行位置服务设置的能力。
自定义地理定位 android 应用需要启用位置服务才能正常工作。
【问题讨论】:
标签:
android
geolocation
gps
location
【解决方案1】:
您可以检查 GPS 是否启用,如果未启用则显示消息
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//here do what you want when the GPS service is enabled
Toast.makeText(MainActivity.this, "is enable", Toast.LENGTH_SHORT).show();
} else {
MaterialAlertDialogBuilder locationDialog = new MaterialAlertDialogBuilder(MainActivity.this);
locationDialog.setTitle("Attention");
locationDialog.setMessage("Location settings must be enabled from the settings to use the application");
locationDialog.setCancelable(false);
locationDialog.setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
locationDialog.create().show();
}
}
}