0

这是我用于检查我的应用程序的网络连接的代码。我希望我的应用程序仅在连接到网络时运行,如果没有则关闭它。代码运行没有错误,但问题是 alertdialog 多次显示。

private void checkConnectivity(){

      final Thread checkConnection = new Thread(new Runnable(){
            @Override
            public void run()
            {

                while (checkCon == true){
                if(!isNetworkAvailable(MainActivity.this)) {

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            new AlertDialog.Builder(MainActivity.this)
                            .setMessage("No network connection.")   
                            .setCancelable(false)
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface d, int which) {                                 
                                    checkCon = false;
                                    finish();

                                }
                             }).show();                                                         


                        }
                    });                         
                    } else {
                        checkCon = true;
                    }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                }}
        });
        checkConnection.start();


  }
4

2 回答 2

0

在当前的Activityor中添加一个新方法Fragment

private boolean isNetworkAvailable(){
        boolean available = false;
        /** Getting the system's connectivity service */
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        /** Getting active network interface  to get the network's status */
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if(networkInfo !=null && networkInfo.isAvailable())
            available = true;

        /** Returning the status of the network */
        return available;
    }

这里是如何使用它。您可以在onCreate()方法内部使用它:

if (isNetworkAvailable() == true){ // if network is available
   // do your thing here
   ...
}else{ // "else" means that the network is not available
   // do your thing here
   ...
}
于 2014-12-31T11:38:08.193 回答
0

通过harism回答,谢谢

  private void checkConnectivity(){

      final Thread checkConnection = new Thread(new Runnable(){
            @Override
            public void run()
            {

                while (checkCon == true){
                if(!isNetworkAvailable(MainActivity.this)) {

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            new AlertDialog.Builder(MainActivity.this)
                            .setMessage("No network connection.")   
                            .setCancelable(false)
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface d, int which) {                                 

                                    finish();                                       
                                }

                             }).show(); 
                             checkCon = false;
                        }
                    });                         
                    } else {
                        checkCon = true;
                    }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                }}
        });
        checkConnection.start();


  }
于 2014-12-31T08:13:03.937 回答