Android小技巧——弹窗询问是否打开GPS

private LocationManager locationManager;
private void initGPS() { locationManager = (LocationManager) this.getSystemService(Context. LOCATION_SERVICE) ; // 判断GPS模块是否开启,如果没有则开启 if (! locationManager.isProviderEnabled(android.location.LocationManager. GPS_PROVIDER)) { final AlertDialog.Builder dialog = new AlertDialog.Builder( this) ; dialog.setTitle( "请打开GPS连接") ; dialog.setMessage( "为了拍照记录位置信息,请先打开GPS") ; dialog.setPositiveButton( "设置" , new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0 , int arg1) { // 转到手机设置界面,用户设置GPS Intent intent = new Intent(Settings. ACTION_LOCATION_SOURCE_SETTINGS) ; Toast. makeText(MainActivity. this, "打开后直接点击返回键即可,若不打开返回下次将再次出现" , Toast. LENGTH_SHORT).show() ; startActivityForResult(intent , 0) ; // 设置完成后返回到原来的界面 } }) ; dialog.setNeutralButton( "取消" , new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0 , int arg1) { initGPS() ; } }) ; dialog.show() ; }} @Override protected void onActivityResult( int requestCode , int resultCode , Intent data) { if(requestCode == 0){ if ( locationManager.isProviderEnabled(android.location.LocationManager. GPS_PROVIDER)){ initGPS() ; } }}

猜你喜欢

转载自blog.csdn.net/wujiaxiangxw/article/details/80227977
今日推荐