2017-07-17 12 views
1

ロケーションサービスがオンになっているかどうかを確認するときにチェックします。そうでない場合は、「ロケーションアクティビティを有効にする」インテントを開始するダイアログを開きます。私はそれから戻ってくると、私はその場所が本当に有効になっているかどうかをチェックしています。もしそうなら、私は警告ダイアログを却下します。onResume()の後にAlertDialogを閉じることができません

理論的にはこれはうまくいくはずですが、私の活動が再開し、dialog.dismiss()に電話をしても絶対に何も起こりません。

私のコードはfollows-の通りです:

@Override 
protected void onResume() { 
    super.onResume(); 
    System.out.println("Activity resume()"); 
    LocationUtils.checkAndEnableLocationServices(this); 
} 

私が行方不明です何:

public class LocationUtils { 

private static AlertDialog dialog_ = null; 

public static void checkAndEnableLocationServices(final Activity context) { 
    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); 
    boolean gps_enabled = false; 
    boolean network_enabled = false; 

    try { 
     gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

    try { 
     network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

    System.out.println("gps_enabled = " + gps_enabled); 
    System.out.println("network_enabled = " + network_enabled); 

    if (!gps_enabled && !network_enabled) { 
     // notify user 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 

     builder.setMessage("Location services are disabled"); 
     builder.setPositiveButton("Enable Location Services", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 

       Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       context.startActivity(myIntent); 
       //get gps 
      } 
     }); 
     builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       context.finish(); 
      } 
     }); 
     //For future reference. 
     AlertDialog dialog = builder.create(); 

     dialog_ = dialog; 

     dialog.show(); 
    } else { 
     if(dialog_!=null) { 
      dialog_.dismiss(); 
     } 
    } 
} 

}私の主な活動で

私はfollowing-を行うonResumeコールバックを持っていますか?なぜこれはダイアログが閉じていないのですか?コードは何のエラーも投げていません。これは私のためのWTFの瞬間です。

答えて

1

をenambledない場合はあなたがローカルのアラートダイアログのメソッドを呼び出しているalertDialog.show OnResumeでそれを表示することができます。

dialog_ = builder.create(); 
dialog_.show 

とonResume()と、

AlertDialog dialog = builder.create(); 
    dialog_ = dialog; 

のコードを置き換え

if(dialog_!=null) { 
     dialog_.dismiss(); 
    } 
0

ヨは、正のボタンがクリックされたときに、ダイアログを閉じ、ロケーションサービスが

+0

は機能しませんでした。すでにそれを試みた –

関連する問題