如果连接可用,我如何检测网络状态的变化并重新加载我的Android应用程序

时间:2013-09-15 14:15:27

标签: android webview wifi

我有一个在互联网连接处于活动状态时运行的Android应用程序。当连接不可用时,会出现一个对话框,通过该对话框可以激活wifi。启动wifi连接后,当连接不可用时,我已经让应用程序退出。问题是,我必须重新加载应用程序并在启用wifi后启用互联网连接时继续使用应用程序而不是退出应用程序。

以下是代码:

if (CheckConnection.checkInternetConnection(this)) {
        //load a webview and show contents from web.
   }

        else
        {
        try {
    AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();

    alertDialog.setTitle("Info");
    alertDialog.setMessage("Internet Connection not available. Please cross check your internet connectivity and try again.");
    alertDialog.setIcon(R.drawable.error);
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.setCancelable(false);



    alertDialog.setButton2("WiFi Setting", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
            startActivity(intent);
            System.exit(0);

        }
     });

    alertDialog.show();
    }
    catch(Exception e)
    {

    }

1 个答案:

答案 0 :(得分:0)

不要调用System.exit(0)。

首先,因为它不是一种推荐的退出应用程序的方法,而是让用户看起来好像崩溃一样。

其次,你为什么要退出?用户将被导航到互联网设置,打开它们,当他们按下时,您的活动已经存在。您可以在活动的重新计算中重新检查互联网状态。这是在您的应用程序中处理不可用的Internet连接的可接受方式。