2015-10-16 57 views
1

此代码能够转到GPS设置并打开它,但按下它并不意图回到应用程序。相反,它将我带到我的发射器的家中。意图打开GPS并返回到应用程序

 public void createDialog(){ 
     AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     alert.setTitle(R.string.gps_message); 
     alert.setPositiveButton(R.string.gps_settings, new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0); 
       return; 
      } 
     }); 

     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       return; 
      } 
     }); 
     alert.create(); 
     alert.show(); 
    } 
+0

您可以发布创建对话框的代码请。 – vguzzi

+0

检查你的logcat。它应该返回到您的应用程序,但也许你的应用程序已退出或崩溃。 –

回答

1

试着改变你的启动代码,以这样的:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 

而且不是调用返回后,尝试将其更改为这样:

dialog.dismiss(); 

调用的返回可能会关闭您的活动。

+0

我试过这个,但它不工作。 :( –

+0

:(你可以发布你用来显示你的对话框的代码,我会看看我能否解决你的问题。 – vguzzi

0

这为我工作:

AlertDialog.Builder notifyLocationServices = new AlertDialog.Builder(PlacesDecoder.this); 
     notifyLocationServices.setTitle("Switch on Location Services"); 
     notifyLocationServices.setMessage("Location Services must be turned on to complete this action. Also please take note that if on a very weak network connection, such as 'E' Mobile Data or 'Very weak Wifi-Connections' it may take even 15 mins to load. If on a very weak network connection as stated above, location returned to application may be null or nothing and cause the application to crash."); 
     notifyLocationServices.setPositiveButton("Ok, Open Settings", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Intent openLocationSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       PlacesDecoder.this.startActivity(openLocationSettings); 
       finish(); 
      } 
     }); 
     notifyLocationServices.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 
     notifyLocationServices.show(); 

注:更改值,以适应自己。 也知道这不会使用结果的开始活动。所以请相应修改它们。例如:班级名称 我希望这可以帮助

相关问题