Android上的ConnectivityManager日志消息泛滥

时间:2016-02-16 13:43:10

标签: android network-programming logcat

我有一个例行检查Android网络连接,每1秒运行一次。

每次检查都会向logcat添加一行。

有没有办法禁用此日志垃圾邮件?

  

02-16 23:36:26.249 32137-32137 / com.xxxxxx.xxx D / ConnectivityManager:   getActiveNetworkInfo:NetworkInfo:type:WIFI [],state:   CONNECTED / CONNECTED,原因:(未指定),漫游:false,故障转移:   false,isAvailable:true,isConnectedToProvisioningNetwork:false

public void updateNetworkStatus() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    // if no network is available networkInfo will be null
    if (networkInfo != null && networkInfo.isConnected()) {
        networkType = networkInfo.getTypeName();    // "", "WIFI", "mobile"
    } else networkType = "";
}

*更新完整代码

package au.com.boxdev.testcon;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private String networkType = "";

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

        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateNetworkStatus();
                    }
                });
            }
        }, 0, 1000);
    }

    public void updateNetworkStatus() {
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();

        // if no network is available networkInfo will be null
        if (networkInfo != null && networkInfo.isConnected()) {
            networkType = networkInfo.getTypeName();    // "", "WIFI", "mobile"
        } else networkType = "";
    }
}

0 个答案:

没有答案