检测系统何时开始下载

时间:2014-11-27 05:56:55

标签: android download android-download-manager

有没有办法检测系统何时开始下载并获取信息并强行将其下载到特定位置?我用谷歌搜索并得出的结果没什么用处

1 个答案:

答案 0 :(得分:1)

启动此服务类,然后开始下载,它将打印结果以获取下载状态。

公共类MyService扩展了Service {

private Timer timer;
private TimerTask timerTask;
private static final int SAMPLING_RATE = 1000;

public MyService() {
}

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

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    Log.e("Download", "onstart");
}

@Override
public void onCreate() {
    timer = new Timer();
    timerTask = new TimerTask() {

        @Override
        public void run() {
            // Log.d(MyService.class.toString(),
            // "tic ... "+System.currentTimeMillis());
            getDownloadData();
        }
    };
    if (timer != null && timerTask != null) {
        timer.schedule(timerTask, 0, SAMPLING_RATE);
    }
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public void getDownloadData() {
    DownloadManager downloadMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_FAILED
            | DownloadManager.STATUS_PENDING
            | DownloadManager.STATUS_RUNNING
            | DownloadManager.STATUS_SUCCESSFUL);
    Cursor c = downloadMgr.query(query);

    if (c == null) {
        System.out.println("--------------------------");
    } else {
        if (c.moveToFirst()) {
            System.out.println("------------End--------------");
            while (c.isAfterLast() == false) {
                getStatus(c);
                c.moveToNext();
            }
        }
    }
}

public void getStatus(Cursor c) {

    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
    int columnReason = c.getColumnIndex(DownloadManager.COLUMN_REASON);

    int reason = c.getInt(columnReason);
    int status = c.getInt(columnIndex);

    String statusText = null;
    String reasonText = null;
    switch (status) {
    case DownloadManager.STATUS_FAILED:
        statusText = "STATUS_FAILED";
        switch (reason) {
        case DownloadManager.ERROR_CANNOT_RESUME:
            reasonText = "ERROR_CANNOT_RESUME";
            break;
        case DownloadManager.ERROR_DEVICE_NOT_FOUND:
            reasonText = "ERROR_DEVICE_NOT_FOUND";
            break;
        case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
            reasonText = "ERROR_FILE_ALREADY_EXISTS";
            break;
        case DownloadManager.ERROR_FILE_ERROR:
            reasonText = "ERROR_FILE_ERROR";
            break;
        case DownloadManager.ERROR_HTTP_DATA_ERROR:
            reasonText = "ERROR_HTTP_DATA_ERROR";
            break;
        case DownloadManager.ERROR_INSUFFICIENT_SPACE:
            reasonText = "ERROR_INSUFFICIENT_SPACE";
            break;
        case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
            reasonText = "ERROR_TOO_MANY_REDIRECTS";
            break;
        case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
            reasonText = "ERROR_UNHANDLED_HTTP_CODE";
            break;
        case DownloadManager.ERROR_UNKNOWN:
            reasonText = "ERROR_UNKNOWN";
            break;
        }
        break;
    case DownloadManager.STATUS_PAUSED:
        statusText = "STATUS_PAUSED";
        switch (reason) {
        case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
            reasonText = "PAUSED_QUEUED_FOR_WIFI";
            break;
        case DownloadManager.PAUSED_UNKNOWN:
            reasonText = "PAUSED_UNKNOWN";
            break;
        case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
            reasonText = "PAUSED_WAITING_FOR_NETWORK";
            break;
        case DownloadManager.PAUSED_WAITING_TO_RETRY:
            reasonText = "PAUSED_WAITING_TO_RETRY";
            break;
        }
        break;
    case DownloadManager.STATUS_PENDING:
        statusText = "STATUS_PENDING";
        break;
    case DownloadManager.STATUS_RUNNING:
        statusText = "STATUS_RUNNING";
        break;
    case DownloadManager.STATUS_SUCCESSFUL:
        statusText = "STATUS_SUCCESSFUL";
        break;
    }

    Log.d("status", statusText + "   " + reasonText);
}

}