Android:开启一个服务循环ping服务器,记录ping 5次均失败触发某条件

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Arrays;

public class MyService extends Service {

    String webPing = "junction.havensphere.com";

    public static boolean PingIptv(String address, int pingTimes, int timeOut) {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("ping " + "-c " + pingTimes + " -w " + timeOut + " " + address);
            InputStreamReader r = new InputStreamReader(process.getInputStream());
            LineNumberReader returnData = new LineNumberReader(r);
            String returnMsg = "";
            String line = "";
            while ((line = returnData.readLine()) != null) {
                System.out.println(line);
                returnMsg += line;
            }
            if (returnMsg.indexOf("100% packet loss") != -1) {
                System.out.println("与 " + address + " 连接不畅通.");
                return false;
            } else {
                System.out.println("与 " + address + " 连接畅通.");
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;

    }


    private ArrayList pingResult = new ArrayList();
    private ArrayList pingList = new ArrayList(Arrays.asList(false, false, false, false, false));


    @Override
    public void onCreate() {
        new Thread(new MyThread()).start();
    }



    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    if (PingIptv(webPing, 2, 2)) {
                        Log.e("TAG", "有网络,确定可用");
                        pingResult.add(true);
                    } else {
                        Log.e("TAG", "网络不可用");
                        pingResult.add(false);
                    }

                    Log.e("waylon-target", pingList.size() + "");
                    Log.e("waylon-state", pingResult.size() + "");

                    //判断是否有5次
                    if (pingResult.size() == 5) {
                        Log.e("waylon-state", "It was connected five times");
                        //5次相同
                        if (pingResult.equals(pingList)) {
                            //如果相同,就
                            Intent intent = new Intent();
                            intent.setAction("com.tpv.xmic.SOURCECHANGE");
                            intent.putExtra("source", "1");
                            intent.putExtra("name", "Iauncher");
                            sendBroadcast(intent);

                            Intent intent2 = new Intent("mstar.tvsetting.ui.intent.action.RootActivity");
                            intent2.putExtra("task_tag", "input_source_changed");
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);
                        } else {
                            Log.e("waylon-state", "Five times is different");
                            pingResult.clear();
                        }
                    }
                    break;
            }
        }
    };



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public class MyThread implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(5000);// 线程暂停10秒,单位毫秒
                    Message message = new Message();
                    message.what = 1;
                    Thandler.sendMessage(message);// 发送消息
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}



你可能感兴趣的:(android,服务器,运维)