【Android】- Android判断GPS定位是否打开弹出对话框
16lz
2021-01-23
最近在做旅游项目,需要用到地图的定位导航,为了提高定位的精确度,在不打开GPS和打开GPS时,定位的偏差较大,
以下是在未打开GPS时弹出对话框提示用户设置GPS的相关代码:
/** * 判断GPS是否开启 */private void initGPS() { LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //判断GPS是否开启,没有开启,则开启 if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ openGPSDialog(); }}
/** * 打开GPS对话框 */private void openGPSDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请打开GPS连接") .setIcon(R.mipmap.ico_gps) .setMessage("为了提高定位的准确度,更好的为您服务,请打开GPS") .setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //跳转到手机打开GPS页面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); //设置完成后返回原来的界面 startActivityForResult(intent,0); } }) .setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }).show();}效果如下所示:
更多相关文章
- Android 2.3禁止系统弹出应用程序强制退出对话框
- Android中自定义对话框(Dialog)
- Android常用控件--TimePickerDialog(时间选择对话框)
- Android 对话框大合集
- android在Service中弹出Dialog对话框,即全局性对话框
- Android中的对话框的使用技巧
- Android 第七天重置版_样式主题国际化动画对话框
- Android AlertDialog 对话框 ProgressDialog