android - 互联网连接检查线程 Android

标签 android multithreading internet-connection

在我的主要 Activity 中,有一个互联网状态 TextView。在那个 TextView 中,我想显示是否启用了互联网连接。我必须每 10 秒刷新一次状态。

我知道我必须在单独的线程中执行此操作,但我尝试了很多。我没有得到完美的解决方案。

public class MainActivity extends Activity {
ImageView imageView;
TextView internetStausTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    internetStausTextView = (TextView) findViewById(R.id.tv1);

    new InternetChecker();
}

public class InternetChecker implements Runnable {
    Thread t;
    boolean internetStatus;
    ConnectivityManager conMgr;

    public InternetChecker() {
        t = new Thread(this);
        t.start();
        conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    public void run() {
        while (true) {

            if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                    || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
                internetStatus = true;

            } else if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                    || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
                internetStatus = false;

            }
            runOnUiThread(new Runnable() {
                public void run() {
                    if (internetStatus)
                        internetStausTextView.setText("connected");
                    else
                        internetStausTextView.setText("Not connected.");
                }
            });

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {

            }
        }
    }
}

这是我的代码.... 但这会影响应用程序的性能。

谁能帮我把子类 InternetChecker 取出到一个单独的文件中。

最佳答案

试试这个:

已编辑:

首先创建一个HandlerThread运行标志:

Handler mHandler = new Handler();
boolean isRunning = true;

然后,使用 onCreate() 方法中的这个线程:

new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (isRunning) {
                    try {
                        Thread.sleep(10000);
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                // Write your code here to update the UI.
                                displayData();
                            }
                        });
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }).start(); 

然后,声明您的处理程序每​​ 10 秒调用一次的方法:

private void displayData() {
        ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nf=cn.getActiveNetworkInfo();
        if(nf != null && nf.isConnected()==true )
        {
            Toast.makeText(this, "Network Available", Toast.LENGTH_SHORT).show();
            myTextView.setText("Network Available");
        }
        else
        {
            Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT).show();
            myTextView.setText("Network Not Available");
        }       
    } 

要停止线程调用这个:

isRunning = false;

就是这样。

谢谢。

关于android - 互联网连接检查线程 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037455/

相关文章:

java - Jersey:异步请求后立即响应

c# - 如何确保特定的后台工作线程在另一个后台工作线程之前执行?

android - android模拟器中的互联网连接总是显示连接状态

python - 如何查看电脑是否联网?

android - 检测android设备是否连接到互联网

android - 如何在 Android 中打开新屏幕

python - 计时器无法从另一个线程停止 - 删除焦点

android - 尝试创建自定义 View.onFocusChangeListener 时出错

java - 在android中的两个 Activity 之间保持数据更新

android - 如何放大Android Layout Editor?