检查网络连接android

时间:2014-12-31 08:00:21

标签: android multithreading network-connection

这是我的代码,用于检查我的应用程序的网络连接。我希望我的应用程序仅在连接到网络时运行,如果没有则关闭它。代码运行时没有错误,但问题是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();


  }

2 个答案:

答案 0 :(得分: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();


  }

答案 1 :(得分:0)

在当前ActivityFragment

中添加新方法
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
   ...
}