2017-02-05 11 views
1

私は、インターネットに接続が検出されなかった場合にユーザーにメッセージを表示するアラートダイアログボックスを持っています。インターネットに接続されているかどうかを確認して再試行します。まだ接続されていない場合は、ダイアログボックスが再度表示されますか?あなたの方法Overriteアラートダイアログインターネットに接続されているかどうか再確認するには

public class Splash extends Activity { 
private ProgressBar mProgress; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.splash); 

    if (Internet()) { 
     splash(); 
    } else { 
     showAlertDialog(Splash.this, "No Internet Access", 
       "No Internet Connection detected", false); 
    } 
} 

public void splash() { 
    mProgress = (ProgressBar) findViewById(R.id.progress); 
    mProgress.getProgressDrawable().setColorFilter(
      Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN); 

    Thread timerTread = new Thread() { 
     public void run() { 
      try { 
       prog(); 

       sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } finally { 
       Intent intent = new Intent(getApplicationContext(), Games.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
       finish(); 
      } 
     } 
    }; 
    timerTread.start(); 
} 
private void prog() { 
    for (int progress = 0; progress <= 100; progress += 20) { 
     try { 
      Thread.sleep(1000); 
      mProgress.setProgress(progress); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public boolean Internet() { 
    ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext() 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivityManager != null) { 
     NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); 
     if (info != null) 
      for (int i = 0; i < info.length; i++) 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
        return true; 
       } 

    } 
    return false; 
} 

public void showAlertDialog(Context context, String title, String message, Boolean status) { 
    final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

    alertDialog.setTitle(title); 
    alertDialog.setMessage(message); 
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Retry ", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 

    } 
}); 
    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit ", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      finish(); 
      System.exit(0); 
     } 
    }); 
    alertDialog.show(); 


} 

}

+0

をあなたはボタンを作るのではなく、接続のための継続的なチェックのための放送受信機を使用していないのはなぜ? – W4R10CK

答えて

1
 public void showAlertDialog(Context context, String title, String message, Boolean status) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
    alertDialog.positiveText("Retry"); 
     alertDialog.negativeText("Cancel"); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 
     alertDialog.show(); 
    } 

とダイアログ "positiveText" と "negativeText" に追加します。その後、 "onPositive"メソッドを呼び出すことができます。ここでは、インターネット接続がある場合に再試行できます。詳細については

はこのサイトを参照してください:このようなhttps://developer.android.com/guide/topics/ui/dialogs.html

0

変更してコードを..

class Splash extends Activity { 
    private ProgressBar mProgress; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     setContentView(R.layout.splash); 

     checkAndStartActivity(); 
    } 

    public void checkAndStartActivity(
    if (isWorkingInternetPersent()) { 
      splash(); 
     } else { 
      showAlertDialog(Splash.this, "No Internet Access", 
        "No Internet Connection detected", false); 
     } 
    } 


    public void splash() { 
     mProgress = (ProgressBar) findViewById(R.id.progress); 
     mProgress.getProgressDrawable().setColorFilter(
       Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN); 

     Thread timerTread = new Thread() { 
      public void run() { 
       try { 
        prog(); 

        sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        Intent intent = new Intent(getApplicationContext(), Games.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }; 
     timerTread.start(); 
    } 
    private void prog() { 
     for (int progress = 0; progress <= 100; progress += 20) { 
      try { 
       Thread.sleep(1000); 
       mProgress.setProgress(progress); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public boolean isWorkingInternetPersent() { 
     ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext() 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivityManager != null) { 
      NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); 
      if (info != null) 
       for (int i = 0; i < info.length; i++) 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
         return true; 
        } 

     } 
     return false; 
    } 

    public void showAlertDialog(Context context, String title, String message, Boolean status) { 
     AlertDialog.Builder adb = new AlertDialog.Builder(context); 
     adb.setView(alertDialogView); 
     adb.setTitle(title); 

     adb.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      checkAndStartActivity(); 
     } }); 
     adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener({ 
     public void onClick(DialogInterface dialog, int which) { 
     Splash.this.finish(); 
     } }); 
     adb.show(); 

    } 
関連する問題