2017-02-05 9 views
1

전화 상에 인터넷 연결이 감지되지 않은 경우 사용자에게 알리는 대화 상자가 나타납니다. 인터넷 연결이 있는지 확인한 후 다시 시도하고 대화 상자가 다시 나타나면 연결이 끊어지지 않았습니까?경고 대화 상자 인터넷에 연결되어 있는지 다시 확인하는 방법

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(); 


} 
당신의 방법 Overrite

}

+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(); 

    }