STATUS_RUNNING源码实例Demo

android.app.DownloadManager#STATUS_RUNNING源码实例Demo

下面列出了android.app.DownloadManager#STATUS_RUNNING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: EdXposedManager   文件: DownloadView.java
@Override
public void run() {
    while (true) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            return;
        }

        try {
            mInfo = DownloadsUtil.getById(getContext(), mInfo.id);
        } catch (NullPointerException ignored) {
        }

        refreshView();
        if (mInfo == null)
            return;

        if (mInfo.status != DownloadManager.STATUS_PENDING
                && mInfo.status != DownloadManager.STATUS_PAUSED
                && mInfo.status != DownloadManager.STATUS_RUNNING)
            return;
    }
}
 
源代码2 项目: OpenMapKitAndroid   文件: OSMDownloader.java
protected void pollDownloadManager() {
    while (downloading) {
        DownloadManager.Query q = new DownloadManager.Query();
        q.setFilterById(downloadId);
        Cursor cursor = downloadManager.query(q);
        cursor.moveToFirst();
        final int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
        final String msg = PROGRESS_MSG + ((double)bytesDownloaded) / 1000000.0 + " MB";
        publishProgress(msg);
        status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
        statusMessage = statusMessage(cursor, bytesDownloaded);
        Log.d("OSMDownloader", statusMessage);
        if (status != DownloadManager.STATUS_PENDING && status != DownloadManager.STATUS_RUNNING) {
            downloading = false;
        }
        // throttle the thread
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
}
 
源代码3 项目: arcusandroid   文件: ClipDownloadControllerImpl.java
protected void doGetStatus(long downloadId) {
    try {
        switch (getDownloadManagerStatus(downloadId)) {
            case DownloadManager.STATUS_FAILED:
                onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_CANCELED);
                removeCurrentDownloadReferences();
                break;

            case DownloadManager.STATUS_SUCCESSFUL:
                onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
                removeCurrentDownloadReferences();
                break;

            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                int progress = doGetDownloadProgress();
                if (progress == 100) {
                    onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
                    removeCurrentDownloadReferences();
                }
                else {
                    onDownloadProgressChanged(progress, STATUS_DOWNLOAD_RUNNING);
                }
                break;
        }
    }
    catch (Exception ex) {
        removeCurrentDownloadReferences();
        onDownloadFatalError(ex);
    }
}
 
源代码4 项目: arcusandroid   文件: ClipDownloadControllerImpl.java
protected boolean downloadInProgress() {
    long downloadId = getDownloadID();
    if (downloadId == -1) {
        return false;
    }

    try {
        switch (getDownloadManagerStatus(downloadId)) {
            case DownloadManager.STATUS_FAILED:
                removeCurrentDownloadReferences();
                return false;

            case DownloadManager.STATUS_SUCCESSFUL:
                removeCurrentDownloadReferences();
                return false;

            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                return true;

            default:
            case -1:
                return false;
        }
    }
    catch (Exception ex) {
        logger.error("Could not process download ID [{}]", downloadId, ex);
    }

    return false;
}
 
源代码5 项目: FirefoxReality   文件: Download.java
public static Download from(Cursor cursor) {
    Download download = new Download();
    download.mId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
    download.mUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
    int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
    switch (status) {
        case DownloadManager.STATUS_RUNNING:
            download.mStatus = RUNNING;
            break;
        case DownloadManager.STATUS_FAILED:
            download.mStatus = FAILED;
            break;
        case DownloadManager.STATUS_PAUSED:
            download.mStatus = PAUSED;
            break;
        case DownloadManager.STATUS_PENDING:
            download.mStatus = PENDING;
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            download.mStatus = SUCCESSFUL;
            break;
        default:
            download.mStatus = UNAVAILABLE;
    }
    download.mMediaType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
    download.mTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));
    download.mOutputFile = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    download.mDescription = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION));
    download.mSizeBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
    download.mDownloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
    download.mLastModified = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
    download.mReason = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
    return download;
}
 
源代码6 项目: WayHoo   文件: DownloadNewVersionJob.java
@SuppressWarnings("static-access")
private boolean checkDownloadRunning() {
	UpgradeInfo prefUpgradeInfo = Preferences.getUpgradeInfo(mContext);
	String version = prefUpgradeInfo.getVersion();
	int downloadStatus = Preferences.getDownloadStatus(mContext);
	if (version != null && version.trim().length() != 0
			&& version.equals(mUpgradeInfo.getVersion())) {
		long downloadId = Preferences.getDownloadId(mContext);
		if (downloadId != -1) {
			final DownloadManager downloadManager = (DownloadManager) mContext
					.getSystemService(mContext.DOWNLOAD_SERVICE);
			DownloadManager.Query mDownloadQuery = new DownloadManager.Query();
			mDownloadQuery.setFilterById(downloadId);
			Cursor cursor = downloadManager.query(mDownloadQuery);
			if (cursor != null && cursor.moveToFirst()) {
				int status = cursor.getInt(cursor
						.getColumnIndex(DownloadManager.COLUMN_STATUS));
				if (status == DownloadManager.STATUS_RUNNING
						|| downloadStatus == Constants.DOWNLOAD_STATUS_RUNNING) {
					return true;
				}
			}

		}
	}
	return false;
}
 
源代码7 项目: IslamicLibraryAndroid   文件: SplashActivity.java
private String statusMessage(@NonNull Cursor c) {
    String msg;

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
        case DownloadManager.STATUS_FAILED:
            msg = "DownloadInfo failed";
            break;

        case DownloadManager.STATUS_PAUSED:
            msg = "DownloadInfo paused";
            break;

        case DownloadManager.STATUS_PENDING:
            msg = "DownloadInfo pending";
            break;

        case DownloadManager.STATUS_RUNNING:
            msg = "DownloadInfo in progress";
            break;

        case DownloadManager.STATUS_SUCCESSFUL:
            msg = "DownloadInfo complete";
            break;

        default:
            msg = "DownloadInfo is nowhere in sight";
            break;
    }

    return (msg);
}
 
源代码8 项目: WayHoo   文件: DownloadNewVersionJob.java
@SuppressWarnings("static-access")
private boolean checkDownloadRunning() {
	UpgradeInfo prefUpgradeInfo = Preferences.getUpgradeInfo(mContext);
	String version = prefUpgradeInfo.getVersion();
	int downloadStatus = Preferences.getDownloadStatus(mContext);
	if (version != null && version.trim().length() != 0
			&& version.equals(mUpgradeInfo.getVersion())) {
		long downloadId = Preferences.getDownloadId(mContext);
		if (downloadId != -1) {
			final DownloadManager downloadManager = (DownloadManager) mContext
					.getSystemService(mContext.DOWNLOAD_SERVICE);
			DownloadManager.Query mDownloadQuery = new DownloadManager.Query();
			mDownloadQuery.setFilterById(downloadId);
			Cursor cursor = downloadManager.query(mDownloadQuery);
			if (cursor != null && cursor.moveToFirst()) {
				int status = cursor.getInt(cursor
						.getColumnIndex(DownloadManager.COLUMN_STATUS));
				if (status == DownloadManager.STATUS_RUNNING
						|| downloadStatus == Constants.DOWNLOAD_STATUS_RUNNING) {
					return true;
				}
			}

		}
	}
	return false;
}
 
源代码9 项目: OpenHub   文件: Downloader.java
private void checkStatus() {
    //cause SQLiteException at 乐视 LE X820 Android 6.0.1,level 23
    try{
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadId);
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch (status) {
                case DownloadManager.STATUS_PAUSED:
                    break;
                case DownloadManager.STATUS_PENDING:
                    break;
                case DownloadManager.STATUS_RUNNING:
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
                    String tip = mContext.getString(R.string.download_complete)
                            .concat("\n").concat(getFilePath());
                    Toasty.success(mContext, tip).show();
                    unregister();
                    break;
                case DownloadManager.STATUS_FAILED:
                    Toasty.error(mContext, mContext.getString(R.string.download_failed)).show();
                    unregister();
                    break;
            }
        }
        c.close();
    }catch (SQLiteException e){
        Logger.d(e);
        unregister();
    }

}
 
源代码10 项目: OpenMapKitAndroid   文件: DeploymentDownloader.java
private void pollDownloadManager() {
    while (downloading) {
        DownloadManager.Query q = new DownloadManager.Query();
        q.setFilterById(downloadIds);
        Cursor cursor = downloadManager.query(q);
        bytesDownloaded = 0;
        filesCompleted = 0;
        while(cursor.moveToNext()) {
            bytesDownloaded += cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
            if (status != DownloadManager.STATUS_PENDING && status != DownloadManager.STATUS_RUNNING) {
                ++filesCompleted;
            }
        }
        if (!canceled) {
            publishProgress();
        }
        if (deployment.fileCount() == filesCompleted) {
            downloading = false;
        }
        // throttle the thread
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
源代码11 项目: OpenMapKitAndroid   文件: OSMDownloader.java
protected String statusMessage(Cursor c, int bytesDownloaded) {
    String msg;
    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
        case DownloadManager.STATUS_FAILED:
            msg = "Download failed: " + bytesDownloaded + " bytes downloaded.";
            break;

        case DownloadManager.STATUS_PAUSED:
            msg = "Download paused: " + bytesDownloaded + " bytes downloaded.";
            break;

        case DownloadManager.STATUS_PENDING:
            msg = "Download pending: " + bytesDownloaded + " bytes downloaded.";
            break;

        case DownloadManager.STATUS_RUNNING:
            msg = "Download in progress: " + bytesDownloaded + " bytes downloaded.";
            break;

        case DownloadManager.STATUS_SUCCESSFUL:
            msg = "Download complete: " + bytesDownloaded + " bytes downloaded.";
            break;

        default:
            msg = "STATUS MESSAGE ERROR";
            break;
    }
    return (msg);
}
 
@Override
protected HashMap<String, Integer> doInBackground(Void... arg0) {
	// get an array of ids, for use in the download manager query
	Cursor c = dataService.getHelper().getReadableDatabase().rawQuery(sql, null);
	long[] ids = new long[c.getCount()];
	while (c.moveToNext()) {
		ids[c.getPosition()] = c.getLong(c.getColumnIndex("dlm_id"));
	}
	c.close();

	if (ids.length > 0) {
		q.setFilterById(ids);
		q.setFilterByStatus(DownloadManager.STATUS_RUNNING);
		
		Cursor cursor = getDownloadManager().query(q);
		HashMap<String, Integer> update = new HashMap<String, Integer>(cursor.getCount());
		while (cursor.moveToNext()) {
			String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
			String youtubeId = youtubeIdFromFilename(filename);
			int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
			if (youtubeId != null) {
				int pct = -1;
				switch (status) {
				case DownloadManager.STATUS_FAILED:
				case DownloadManager.STATUS_PENDING:
				default:
					pct = 0;
					break;
				case DownloadManager.STATUS_PAUSED:
				case DownloadManager.STATUS_RUNNING:
					long bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
					long size = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
					pct = (int) (size==0? 0: 100 * bytes_downloaded / size);
					break;
				case DownloadManager.STATUS_SUCCESSFUL:
					pct = 100;
					break;
				}
				update.put(youtubeId, pct);
			}
		}
		cursor.close();
		
		return update;
	}
	
	return null;
}
 
源代码13 项目: EdXposedManager   文件: DownloadView.java
@Override
public void run() {
    if (mUrl == null) {
        btnDownload.setVisibility(View.GONE);
        btnSave.setVisibility(View.GONE);
        btnDownloadCancel.setVisibility(View.GONE);
        btnRemove.setVisibility(View.GONE);
        btnInstall.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        txtInfo.setVisibility(View.VISIBLE);
        txtInfo.setText(R.string.download_view_no_url);
    } else if (mInfo == null) {
        btnDownload.setVisibility(View.VISIBLE);
        btnSave.setVisibility(View.VISIBLE);
        btnDownloadCancel.setVisibility(View.GONE);
        btnRemove.setVisibility(View.GONE);
        btnInstall.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        txtInfo.setVisibility(View.GONE);
    } else {
        switch (mInfo.status) {
            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                btnDownload.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnDownloadCancel.setVisibility(View.VISIBLE);
                btnRemove.setVisibility(View.GONE);
                btnInstall.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                txtInfo.setVisibility(View.VISIBLE);
                if (mInfo.totalSize <= 0 || mInfo.status != DownloadManager.STATUS_RUNNING) {
                    progressBar.setIndeterminate(true);
                    txtInfo.setText(R.string.download_view_waiting);
                } else {
                    progressBar.setIndeterminate(false);
                    progressBar.setMax(mInfo.totalSize);
                    progressBar.setProgress(mInfo.bytesDownloaded);
                    txtInfo.setText(getContext().getString(
                            R.string.download_view_running,
                            mInfo.bytesDownloaded / 1024,
                            mInfo.totalSize / 1024));
                }
                break;

            case DownloadManager.STATUS_FAILED:
                btnDownload.setVisibility(View.VISIBLE);
                btnSave.setVisibility(View.VISIBLE);
                btnDownloadCancel.setVisibility(View.GONE);
                btnRemove.setVisibility(View.GONE);
                btnInstall.setVisibility(View.GONE);
                progressBar.setVisibility(View.GONE);
                txtInfo.setVisibility(View.VISIBLE);
                txtInfo.setText(getContext().getString(
                        R.string.download_view_failed, mInfo.reason));
                break;

            case DownloadManager.STATUS_SUCCESSFUL:
                btnDownload.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnDownloadCancel.setVisibility(View.GONE);
                btnRemove.setVisibility(View.VISIBLE);
                btnInstall.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
                txtInfo.setVisibility(View.VISIBLE);
                txtInfo.setText(R.string.download_view_successful);
                break;
        }
    }
}
 
源代码14 项目: FireFiles   文件: DownloadStorageProvider.java
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
       final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
       final String docId = String.valueOf(id);

       final String displayName = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
       String summary = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
       String mimeType = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
       if (mimeType == null) {
           // Provide fake MIME type so it's openable
           mimeType = "vnd.android.document/file";
       }
       Long size = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
       if (size == -1) {
           size = null;
       }

       final int status = cursor.getInt(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
       switch (status) {
           case DownloadManager.STATUS_SUCCESSFUL:
               break;
           case DownloadManager.STATUS_PAUSED:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_PENDING:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_RUNNING:
               final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
                       DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
               if (size != null) {
                   final long percent = progress * 100 / size;
                   summary = getContext().getString(R.string.download_running_percent, percent);
               } else {
                   summary = getContext().getString(R.string.download_running);
               }
               break;
           case DownloadManager.STATUS_FAILED:
           default:
               summary = getContext().getString(R.string.download_error);
               break;
       }

       int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
       if (mimeType != null && mimeType.startsWith("image/")) {
           flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
       }

       final long lastModified = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

       final RowBuilder row = result.newRow();
       row.add(Document.COLUMN_DOCUMENT_ID, docId);
       row.add(Document.COLUMN_DISPLAY_NAME, displayName);
       row.add(Document.COLUMN_SUMMARY, summary);
       row.add(Document.COLUMN_SIZE, size);
       row.add(Document.COLUMN_MIME_TYPE, mimeType);
       row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
       row.add(Document.COLUMN_FLAGS, flags);
   }
 
源代码15 项目: FireFiles   文件: DownloadStorageProvider.java
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
       final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
       final String docId = String.valueOf(id);

       final String displayName = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
       String summary = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
       String mimeType = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
       if (mimeType == null) {
           // Provide fake MIME type so it's openable
           mimeType = "vnd.android.document/file";
       }
       Long size = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
       if (size == -1) {
           size = null;
       }

       final int status = cursor.getInt(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
       switch (status) {
           case DownloadManager.STATUS_SUCCESSFUL:
               break;
           case DownloadManager.STATUS_PAUSED:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_PENDING:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_RUNNING:
               final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
                       DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
               if (size != null) {
                   final long percent = progress * 100 / size;
                   summary = getContext().getString(R.string.download_running_percent, percent);
               } else {
                   summary = getContext().getString(R.string.download_running);
               }
               break;
           case DownloadManager.STATUS_FAILED:
           default:
               summary = getContext().getString(R.string.download_error);
               break;
       }

       int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
       if (mimeType != null && mimeType.startsWith("image/")) {
           flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
       }

       final long lastModified = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

       final RowBuilder row = result.newRow();
       row.add(Document.COLUMN_DOCUMENT_ID, docId);
       row.add(Document.COLUMN_DISPLAY_NAME, displayName);
       row.add(Document.COLUMN_SUMMARY, summary);
       row.add(Document.COLUMN_SIZE, size);
       row.add(Document.COLUMN_MIME_TYPE, mimeType);
       row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
       row.add(Document.COLUMN_FLAGS, flags);
   }
 
源代码16 项目: Ency   文件: UpdateService.java
/**
 * 检查下载状态
 */
private void checkStatus(Context context) {
    DownloadManager.Query query = new DownloadManager.Query();
    //通过下载的id查找
    query.setFilterById(downloadId);
    Cursor c = dm.query(query);
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        switch (status) {
            //下载暂停
            case DownloadManager.STATUS_PAUSED:
                break;
            //下载延迟
            case DownloadManager.STATUS_PENDING:
                break;
            //正在下载
            case DownloadManager.STATUS_RUNNING:
                break;
            //下载完成
            case DownloadManager.STATUS_SUCCESSFUL:
                //下载完成安装APK
                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "ency.apk");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setDataAndType(uri, "application/vnd.android.package-archive");
                } else {
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                }
                context.startActivity(intent);
                break;
            //下载失败
            case DownloadManager.STATUS_FAILED:
                Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    c.close();
}
 
源代码17 项目: AndroidUpdater   文件: Updater.java
public void download(UpdaterConfig updaterConfig) {

        if (!UpdaterUtils.checkDownloadState(updaterConfig.getContext())) {
            Toast.makeText(updaterConfig.getContext(), R.string.system_download_component_disable, Toast.LENGTH_SHORT).show();
            UpdaterUtils.showDownloadSetting(updaterConfig.getContext());
            return;
        }

        long downloadId = UpdaterUtils.getLocalDownloadId(updaterConfig.getContext());
        Logger.get().d("local download id is " + downloadId);
        if (downloadId != -1L) {
            FileDownloadManager fdm = FileDownloadManager.get();
            //获取下载状态
            int status = fdm.getDownloadStatus(updaterConfig.getContext(), downloadId);
            switch (status) {
                //下载成功
                case DownloadManager.STATUS_SUCCESSFUL:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_SUCCESSFUL");
                    Uri uri = fdm.getDownloadUri(updaterConfig.getContext(), downloadId);

                    if (uri != null) {
                        //本地的版本大于当前程序的版本直接安装
                        if (UpdaterUtils.compare(updaterConfig.getContext(), uri)) {
                            Logger.get().d("start install UI with local apk");
                            UpdaterUtils.startInstall(updaterConfig.getContext(), uri);
                            return;
                        } else {
                            //从FileDownloadManager中移除这个任务
                            fdm.getDM(updaterConfig.getContext()).remove(downloadId);
                        }
                    }
                    //重新下载
                    startDownload(updaterConfig);
                    break;
                //下载失败
                case DownloadManager.STATUS_FAILED:
                    Logger.get().d("download failed " + downloadId);
                    startDownload(updaterConfig);
                    break;
                case DownloadManager.STATUS_RUNNING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_RUNNING");
                    break;
                case DownloadManager.STATUS_PENDING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PENDING");
                    break;
                case DownloadManager.STATUS_PAUSED:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PAUSED");
                    break;
                case STATUS_UN_FIND:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_UN_FIND");
                    startDownload(updaterConfig);
                    break;
                default:
                    Logger.get().d("downloadId=" + downloadId + " ,status = " + status);
                    break;
            }
        } else {
            startDownload(updaterConfig);
        }
    }
 
源代码18 项目: LLApp   文件: DownloadManagerDemo.java
public static boolean isDownloading(int downloadManagerStatus) {
	return downloadManagerStatus == DownloadManager.STATUS_RUNNING
			|| downloadManagerStatus == DownloadManager.STATUS_PAUSED
			|| downloadManagerStatus == DownloadManager.STATUS_PENDING;
}
 
源代码19 项目: trekarta   文件: Index.java
public Index(Context context, SQLiteDatabase mapsDatabase, SQLiteDatabase hillshadesDatabase) {
    mContext = context;
    mMapsDatabase = mapsDatabase;
    mHillshadeDatabase = hillshadesDatabase;
    mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

    try {
        Cursor cursor = mMapsDatabase.query(TABLE_MAPS, ALL_COLUMNS_MAPS, WHERE_MAPS_PRESENT, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            int x = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_X));
            int y = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_Y));
            short date = cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_DATE));
            byte version = (byte) cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_VERSION));
            logger.debug("index({}, {}, {}, {})", x, y, date, version);
            if (x == -1 && y == -1) {
                mBaseMapVersion = date;
                cursor.moveToNext();
                continue;
            }
            long downloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_DOWNLOADING));
            long hillshadeDownloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_HILLSHADE_DOWNLOADING));
            MapStatus mapStatus = getNativeMap(x, y);
            mapStatus.created = date;
            mapStatus.hillshadeVersion = version;
            int status = checkDownloadStatus(downloading);
            if (status == DownloadManager.STATUS_PAUSED
                    || status == DownloadManager.STATUS_PENDING
                    || status == DownloadManager.STATUS_RUNNING) {
                mapStatus.downloading = downloading;
                logger.debug("  map downloading: {}", downloading);
            } else {
                downloading = 0L;
                setDownloading(x, y, downloading, hillshadeDownloading);
                logger.debug("  cleared");
            }
            status = checkDownloadStatus(hillshadeDownloading);
            if (status == DownloadManager.STATUS_PAUSED
                    || status == DownloadManager.STATUS_PENDING
                    || status == DownloadManager.STATUS_RUNNING) {
                mapStatus.hillshadeDownloading = hillshadeDownloading;
                logger.debug("  hillshade downloading: {}", downloading);
            } else {
                hillshadeDownloading = 0L;
                setDownloading(x, y, downloading, hillshadeDownloading);
                logger.debug("  cleared");
            }
            if (date > 0)
                mLoadedMaps++;
            cursor.moveToNext();
        }
        cursor.close();
    } catch (SQLiteException e) {
        logger.error("Failed to read map index", e);
        mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_CREATE_MAPS);
        mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_INDEX_MAPS);
    }
    mHasHillshades = DatabaseUtils.queryNumEntries(mHillshadeDatabase, TABLE_TILES) > 0;

    //TODO Remove old basemap file
}
 
源代码20 项目: ApkTrack   文件: AppViewHolder.java
/**
 * Sets the right status icon when the app is currently downloading or has downloaded an APK.
 * @param info The object containing the information about the download.
 * @param ctx The context of the application.
 * @return Whether an icon was set.
 */
private boolean _set_action_icon_download(final DownloadInfo info, final Context ctx)
{
    switch (info.get_status())
    {
        case DownloadManager.STATUS_SUCCESSFUL: // APK was downloaded. Install on click.
            final File apk = new File(Uri.parse(info.get_local_uri()).getPath());
            if (apk.exists())
            {
                _action_icon.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.install));
                _action_icon.setVisibility(View.VISIBLE);
                _action_icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        Uri apk_uri;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                        {
                            // Starting from Android N, file:// ACTION_VIEW intents can no longer be passed to other apps.
                            apk_uri = FileProvider.getUriForFile(ctx, BuildConfig.APPLICATION_ID + ".provider", apk);
                        }
                        else
                        {
                            // However, it seems that no system component handles content://[...].apk in previous
                            // versions, which is why the URI is still passed the old way here.
                            apk_uri = Uri.parse(info.get_local_uri());
                        }
                        i.setDataAndType(apk_uri, "application/vnd.android.package-archive");
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        if (i.resolveActivity(ctx.getPackageManager()) != null) {
                            ctx.startActivity(i);
                        }
                        else
                        {
                            Log.v(MainActivity.TAG, "Could not find anyone to receive ACTION_VIEW for " +
                                    "the downloaded APK. (" + info.get_local_uri() + ")");
                        }
                    }
                });
                return true;
            }
            else { // For some reason the APK is not present anymore. Remove the download information.
                return false;
            }
        case DownloadManager.STATUS_PENDING:
        case DownloadManager.STATUS_RUNNING:
            _action_icon.setImageDrawable(ContextCompat.getDrawable(ctx, android.R.drawable.stat_sys_download));
            // Fix the icon color for the white background.
            _action_icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
            ((Animatable) _action_icon.getDrawable()).start();
            _action_icon.setVisibility(View.VISIBLE);
            return true;
        default:
            return false;
    }
}