【问题标题】:Alert Dialog how do i recheck if there is an internet connection警报对话框我如何重新检查是否有互联网连接
【发布时间】:2017-02-05 12:49:23
【问题描述】:

我有一个警告对话框,如果手机上没有检测到互联网连接,它会向用户显示一条消息。如果有互联网连接,我如何检查并重试,如果仍然没有连接,对话框会再次出现?

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


}

}

【问题讨论】:

  • 为什么不使用广播接收器连续检查连接,而不是制作按钮?

标签: android android-alertdialog


【解决方案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”方法,如果有 Internet 连接,您可以再试一次。

欲了解更多信息,请访问此网站:https://developer.android.com/guide/topics/ui/dialogs.html

【讨论】:

    【解决方案2】:

    像这样改变你的代码..

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

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 2019-12-04
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      相关资源
      最近更新 更多