安卓跳转到GPS设置界面

 
 
  1.     /** 
  2.      * 监听GPS 
  3.      */  
  4.     private void initGPS() {  
  5.         LocationManager locationManager = (LocationManager) this  
  6.                 .getSystemService(Context.LOCATION_SERVICE);  
  7.         // 判断GPS模块是否开启,如果没有则开启  
  8.         if (!locationManager  
  9.                 .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {  
  10.             Toast.makeText(TrainDetailsActivity.this, "请打开GPS",  
  11.                     Toast.LENGTH_SHORT).show();  
  12.             AlertDialog.Builder dialog = new AlertDialog.Builder(this);  
  13.             dialog.setMessage("请打开GPS");  
  14.             dialog.setPositiveButton("确定",  
  15.                     new android.content.DialogInterface.OnClickListener() {  
  16.   
  17.                         @Override  
  18.                         public void onClick(DialogInterface arg0, int arg1) {  
  19.   
  20.                             // 转到手机设置界面,用户设置GPS  
  21.                             Intent intent = new Intent(  
  22.                                     Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
  23.                             startActivityForResult(intent, 0); // 设置完成后返回到原来的界面  
  24.   
  25.                         }  
  26.                     });  
  27.             dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() {  
  28.                   
  29.                 @Override  
  30.                 public void onClick(DialogInterface arg0, int arg1) {  
  31.                     arg0.dismiss();  
  32.                 }  
  33.             } );  
  34.             dialog.show();  
  35.         } else {  
  36.             // 弹出Toast  
  37. //          Toast.makeText(TrainDetailsActivity.this, "GPS is ready",  
  38. //                  Toast.LENGTH_LONG).show();  
  39. //          // 弹出对话框  
  40. //          new AlertDialog.Builder(this).setMessage("GPS is ready")  
  41. //                  .setPositiveButton("OK", null).show();  
  42.         }  
  43.     }  
 
 
posted @ 2016-12-22 19:46  新感觉  阅读(651)  评论(0编辑  收藏  举报