2015-10-16 59 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(); 

注:更改值,以適應自己。 也知道這不會使用結果的開始活動。所以請相應修改它們。例如:班級名稱 我希望這可以幫助

相關問題