2017-08-13 15 views
0

アクティブなインターネット接続がない場合は、デバイス上のアクティブなインターネット接続を確認するアンドロイドアプリを作成しています。再試行を要求する警告ダイアログがポップアップして終了します。ポジティブボタンをタップしてアラートダイアログを繰り返し表示するにはどうすればいいですか?

ユーザーがリトライボタンをクリックすると、アプリは再びインターネット接続が有効かどうかを確認します。

私はこのコードをループする際に問題に直面しています。 ここに私のコードは

if(cm.getActiveNetworkInfo()==null){ 
      AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SplashActivity.this); 

      alertBuilder.setMessage("No internet connection").setCancelable(false); 

      alertBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
        // Want to run this code again 
       } 
      }) 
        .setNegativeButton("Quit", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
          // negetive button action goes here 
         } 
        }); 

      AlertDialog alertDialog = alertBuilder.create(); 
      alertDialog.setTitle("Rates and Reviews"); 
      alertDialog.show(); 

     } 
+1

すべてをメソッドにラップし、ポジティブボタンが押されたときにそのメソッドを再度呼び出すのはなぜですか? –

答えて

0

このように?

private void checkInternet(){ 

    if(cm.getActiveNetworkInfo()==null){ 
     AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SplashActivity.this); 

     alertBuilder.setMessage("No internet connection").setCancelable(false); 

     alertBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
       // Want to run this code again 
       checkInternet(); 
      } 
     }) 
       .setNegativeButton("Quit", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
         // negetive button action goes here 
        } 
       }); 

     AlertDialog alertDialog = alertBuilder.create(); 
     alertDialog.setTitle("Rates and Reviews"); 
     alertDialog.show(); 

    } 
} 
+1

あなたの助けをありがとう、それは私のために働いた。 –

関連する問題