最近在弄网络监测相关的东西,然后在此记录。
不多说,来正文。
我在这里采用的是ping来监测网络。如下。
try {
Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 2 " + ipAddress);
int status = process.waitFor();
if (status == 0) {
httpCallbackListener.onSuccess(true);
} else {
httpCallbackListener.onError(false);
}
} catch (IOException e) {
e.printStackTrace();
httpCallbackListener.onError(false);
} catch (InterruptedException e) {
e.printStackTrace();
httpCallbackListener.onError(false);
}
然后为了不阻塞,然后就得异步执行:
/**
* 在这里进行ping操作
* @author Rine
* 2016-9-23
*/
public class ping {
public static void net(final Context context,final String ipAddress, final HttpCallbackListener httpCallbackListener){
new Thread(new Runnable() {
@Override
public void run() {
try {
Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 2 " + ipAddress);
int status = process.waitFor();
if (status == 0) {
httpCallbackListener.onSuccess(true);
} else {
httpCallbackListener.onError(false);
}
} catch (IOException e) {
e.printStackTrace();
httpCallbackListener.onError(false);
} catch (InterruptedException e) {
e.printStackTrace();
httpCallbackListener.onError(false);
}
}
}).start();
}
}
然后HttpCallbackListener类如下:
/**
* 回调(成功与出错)
* @author Rine
* @version 1.0, 2016-1-4
*/
public interface HttpCallbackListener
{
/**
* 成功
* @param finish
*/
void onSuccess(boolean reaponae);
/**
* 出错
* @param error
*/
void onError(boolean reaponae);
}
然后也因为有时候ping中-w 2 其实有时候没有作用,昨天=-=我在有wifi但无网情况下测试一直为30秒,ping才返回值(有wifi但无网络的情况其实很好实现=-=笔记本开个无线,然后把笔记本网断了。手机连上笔记本开的无线。)
然后就是ping的回调:
/**
* 监测网络是否正常
*
* @author Rine
* 2016-9-23
*
*/
public class IsNet {
private Context context;
private TextView text;
public IsNet(Context context, TextView text) {
this.context = context;
this.text = text;
}
private String value = "null";
int time;// 时间
/**计时器线程**/
private ThreadSafe threadSafe ;
public void init(){
time = 0;
threadSafe = new ThreadSafe();
threadSafe.start();
ping.net(context, "www.baidu.com", new HttpCallbackListener() {
@Override
public void onSuccess(boolean reaponae) {
time = -1;
if(value.equals("null")){
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}else if(value.equals("fail")){
value = "null";
}
}
@Override
public void onError(boolean reaponae) {
time = -1;
if(value.equals("null")){
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
}else if(value.equals("fail")){
value = "null";
}
}
});
}
/**
* 计时线程
* (因为有可能ping中w -2有时候不起作用,所以在加了个线程)
*/
public class ThreadSafe extends Thread {
public String mark;
public void run() {
//5秒是超时判断,即如果ping如果5秒内没有返回值,则判断为无网状态。
while(time != -1 && time <= 5)
{
try {
Thread.sleep(1000);
if(time != -1){
time++;
}
if(time == 5){
value = "fail";
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
} catch (InterruptedException e) {
e.printStackTrace();
break;//捕获到异常之后,执行break跳出循环。
}
}
}
}
Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 1){
Toast.makeText(context, "有网", Toast.LENGTH_SHORT).show();
text.setText("有网");
}else if(msg.what == 2){
Toast.makeText(context, "无网", Toast.LENGTH_SHORT).show();
text.setText("无网");
}else if(msg.what == 3){
Toast.makeText(context, "无网络", Toast.LENGTH_LONG).show();
text.setText("无网络");
threadSafe.interrupt();
}
};
};
}
然后就是Main.
public class MainActivity extends Activity {
private Context context;
private IsNet isNet;
private Button bt;
/**有无网络**/
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
bt = (Button) findViewById(R.id.bt);
text = (TextView) findViewById(R.id.text);
isNet = new IsNet(context, text);
init();
}
private void init(){
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//在wifi情况下监测
isNet.init();
});}}
恩。就到这里结束了♪(^∇^*)。
然后下面是DEMO地址,是eclipse版本的=-=
demo地址