package com.tofly.ghrq_route.module.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.IBinder;

import com.tofly.ghrq_route.common.util.net.NetUtil;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zombiexq on 2016/6/13.
 */
public class NetStateService extends Service {

    // 实时监听网络状态改变
    private BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
            {
                Timer timer = new Timer();
                timer.schedule(new QunXTask(getApplicationContext()), new Date());
            }
        }
    };

    public interface GetConnectState
    {
        public void GetState(boolean isConnected); // 网络状态改变之后，通过此接口的实例通知当前网络的状态，此接口在Activity中注入实例对象
    }

    private GetConnectState onGetConnectState;

    public void setOnGetConnectState(GetConnectState onGetConnectState)
    {
        this.onGetConnectState = onGetConnectState;
    }

    private Binder binder = new MyBinder();

    private boolean isContected = true;

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

    @Override
    public void onCreate()
    {// 注册广播
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); // 添加接收网络连接状态改变的Action
        registerReceiver(mReceiver, mFilter);
    }

    class QunXTask extends TimerTask
    {
        private Context context;

        public QunXTask(Context context)
        {
            this.context = context;
        }

        @Override
        public void run()
        {
            if (NetUtil.getNetworkState(context)!=NetUtil.NETWORN_NONE)
            {
                isContected = true;
            }
            else
            {
                isContected = false;
            }
            if (onGetConnectState != null)
            {
                onGetConnectState.GetState(isContected); // 通知网络状态改变
            }
        }

    }

    public class MyBinder extends Binder
    {
        public NetStateService getService()
        {
            return NetStateService.this;
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mReceiver); // 删除广播
    }
}