DownLoadService.java 6.57 KB
package com.cnlive.downloadfile.download.limit;

import android.annotation.SuppressLint;
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.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import com.cnlive.downloadfile.download.util.NetWorkUtil;

import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_MOBILE;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_WIFI;

/**
 * 下载服务
 */
public class DownLoadService extends Service {
    private NetConnectionReceiver netConnectionReceiver;
    private DownloadInfo info = null;
    public static final String ACTION_START = "ACTION_START";
    public static final String ACTION_WAITING = "ACTION_WAITING";
    public static final String ACTION_PAUSE = "ACTION_PAUSE";
    public static final String ACTION_ALLPAUSE = "ACTION_ALLPAUSE";
    public static final String ACTION_ALLSTART = "ACTION_ALLSTRAT";
    public static final String ACTION_DELECT = "ACTION_DELECT";
    public static final String ACION_CLEAR = "ACION_CLEAR";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //判断Intent的值,获得Activity传来的参数
        if(intent != null){
            info = (DownloadInfo) intent.getSerializableExtra("fileUrl");
        }else {
            return super.onStartCommand(intent, flags, startId);
        }

        if(intent.getAction().equals(ACTION_DELECT)){
            //取消
            mHandle.obtainMessage(4, info).sendToTarget();
        }else if(intent.getAction().equals(ACION_CLEAR)){
            //清空
            mHandle.obtainMessage(5).sendToTarget();
        }

        if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_UN_NETWORK)){
            Toast.makeText(this,"无网络",Toast.LENGTH_SHORT).show();
            return super.onStartCommand(intent, flags, startId);
        }

        if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_WIFI)){
            if (intent.getAction().equals(ACTION_START)) {
                //启动初始化线程
                new MyThread(info).start();
            }else if(intent.getAction().equals(ACTION_PAUSE)){
                //暂停
                mHandle.obtainMessage(1, info).sendToTarget();
            }else if(intent.getAction().equals(ACTION_ALLPAUSE)){
                //全部暂停
                mHandle.obtainMessage(2).sendToTarget();
            }else if(intent.getAction().equals(ACTION_ALLSTART)){
                //全部开始
                mHandle.obtainMessage(3).sendToTarget();
            }else if(intent.getAction().equals(ACTION_WAITING)){
                mHandle.obtainMessage(6,info).sendToTarget();
            }
        }

        if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_MOBILE)){
            //弹框...保存用户习惯,拒绝或者是允许,允许的话是仅允许一次还是都允许
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        netConnectionReceiver = new NetConnectionReceiver();
        registerReceiver(netConnectionReceiver, filter);
    }

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

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

    //用于Service和MyThread之间的通信
    @SuppressLint("HandlerLeak")
    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            DownloadInfo info = (DownloadInfo) msg.obj;
            switch (msg.what) {
                case 0:
                    //启动一个下载任务
                    DownloadLimitManager.getInstance().download(info);
                    break;
                case 1://暂停一个
                    DownloadLimitManager.getInstance().pauseDownload(info,false);
                    break;
                case 2://全部暂停
                    DownloadLimitManager.getInstance().pauseAllDownload();
                    break;
                case 3:
                    DownloadLimitManager.getInstance().startAllDownload();
                    break;
                case 4:
                    DownloadLimitManager.getInstance().cancelDownload(info,true);
                    break;
                case 5:
                    DownloadLimitManager.getInstance().clearDownload();
                    break;
                case 6:
                    DownloadLimitManager.getInstance().pauseToWaitDownload(info);
                    break;
            }
        }
    };


    //子线程  获取文件长度
    class MyThread extends Thread {
        private DownloadInfo fileInfo = null;
        public MyThread(DownloadInfo fileInfo) {
            this.fileInfo = fileInfo;
        }
        public void run() {
            mHandle.obtainMessage(0, fileInfo).sendToTarget();
        }
    }

    private class NetConnectionReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                //网络断开
                if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){
                    DownloadLimitManager.getInstance().pauseErrorDownload();
                }
            } else {
                //网络连接
                //启动初始化线程
                if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){
                    new MyThread(DownloadLimitManager.getInstance().getCurremtDownload()).start();
                }
            }
        }
    }
}