本文整理匯總了Java中android.app.DownloadManager類的典型用法代碼示例。如果您正苦於以下問題:Java DownloadManager類的具體用法?Java DownloadManager怎麽用?Java DownloadManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DownloadManager類屬於android.app包,在下文中一共展示了DownloadManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: openDownload
import android.app.DownloadManager; //導入依賴的package包/類
/**
* Called to open a given download item that is downloaded by the android DownloadManager.
* @param context Context of the receiver.
* @param intent Intent from the android DownloadManager.
*/
private void openDownload(final Context context, Intent intent) {
long ids[] =
intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
if (ids == null || ids.length == 0) {
DownloadManagerService.openDownloadsPage(context);
return;
}
long id = ids[0];
Uri uri = DownloadManagerDelegate.getContentUriFromDownloadManager(context, id);
if (uri == null) {
// Open the downloads page
DownloadManagerService.openDownloadsPage(context);
} else {
String downloadFilename = IntentUtils.safeGetStringExtra(
intent, DownloadNotificationService.EXTRA_DOWNLOAD_FILE_PATH);
boolean isSupportedMimeType = IntentUtils.safeGetBooleanExtra(
intent, DownloadNotificationService.EXTRA_IS_SUPPORTED_MIME_TYPE, false);
Intent launchIntent = DownloadManagerService.getLaunchIntentFromDownloadId(
context, downloadFilename, id, isSupportedMimeType);
if (!DownloadUtils.fireOpenIntentForDownload(context, launchIntent)) {
DownloadManagerService.openDownloadsPage(context);
}
}
}
示例2: downloadAndInstall
import android.app.DownloadManager; //導入依賴的package包/類
private void downloadAndInstall(String url) {
final DownloadManager dManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir("zmtmt", "zmtmt_zhibohao_update");
request.setDescription(getString(R.string.new_version_download));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
}
request.setMimeType("application/vnd.android.package-archive");
request.setVisibleInDownloadsUi(true);
final long reference = dManager.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
upgradeReceiver = new UpgradeBroadcastReceiver(dManager, reference);
registerReceiver(upgradeReceiver, filter);
}
示例3: queryChildDocumentsForManage
import android.app.DownloadManager; //導入依賴的package包/類
@Override
public Cursor queryChildDocumentsForManage(
String parentDocumentId, String[] projection, String sortOrder)
throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
// Delegate to real provider
final long token = Binder.clearCallingIdentity();
Cursor cursor = null;
try {
cursor = mDm.query(
new DownloadManager.Query());//.setOnlyIncludeVisibleInDownloadsUi(true));
//copyNotificationUri(result, cursor);
while (cursor.moveToNext()) {
includeDownloadFromCursor(result, cursor);
}
} finally {
IoUtils.closeQuietly(cursor);
Binder.restoreCallingIdentity(token);
}
return result;
}
示例4: onDownloadStart
import android.app.DownloadManager; //導入依賴的package包/類
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
listener.onDownloadStart();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimetype);
//------------------------COOKIE!!------------------------
CookieManager.getInstance().setCookie(url, getCookie());
String cookiesAlt = CookieManager.getInstance().getCookie(url);
request.addRequestHeader(Constant.COOKIE, cookiesAlt);
//------------------------COOKIE!!------------------------
request.addRequestHeader(Constant.USER_AGENT, userAgent);
request.setDescription(activity.getString(R.string.downloading_file));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) activity.getSystemService(activity.DOWNLOAD_SERVICE);
try {
dm.enqueue(request);
activity.showToast(R.string.downloading_file);
} catch (Exception e) {
activity.showToast(R.string.download_failed);
}
}
示例5: loadPoems
import android.app.DownloadManager; //導入依賴的package包/類
public static void loadPoems(final Context context, final MainPresenter presenter) {
File poems_file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
"poems.json");
File poems_old_file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
"poems_old.json");
if (poems_file.exists()) {
poems_file.renameTo(poems_old_file);
}
String url = "https://almoturg.com/poems.json";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Sprog poems");
request.setTitle("Sprog");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,
"poems.json");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
receiver = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) != downloadID){return;}
context.unregisterReceiver(PoemsLoader.receiver);
PoemsLoader.receiver = null;
presenter.downloadComplete();
}
};
context.registerReceiver(receiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadID = manager.enqueue(request);
}
示例6: cancelAllDownloads
import android.app.DownloadManager; //導入依賴的package包/類
public static void cancelAllDownloads(Context context){
if (receiver != null) {
context.unregisterReceiver(receiver);
receiver = null;
}
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(
DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|
DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING);
Cursor cur = manager.query(query);
while (cur.moveToNext()){
manager.remove(cur.getLong(cur.getColumnIndex(DownloadManager.COLUMN_ID)));
}
cur.close();
}
示例7: removeOldDownloads
import android.app.DownloadManager; //導入依賴的package包/類
private void removeOldDownloads(Context context) {
try {
Log.i(TAG, "Removing the old downloads in progress of the previous keyboard version.");
final DownloadManagerWrapper downloadManagerWrapper = new DownloadManagerWrapper(
context);
final DownloadManager.Query q = new DownloadManager.Query();
// Query all the download statuses except the succeeded ones.
q.setFilterByStatus(DownloadManager.STATUS_FAILED
| DownloadManager.STATUS_PAUSED
| DownloadManager.STATUS_PENDING
| DownloadManager.STATUS_RUNNING);
final Cursor c = downloadManagerWrapper.query(q);
if (c != null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
final long downloadId = c
.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
downloadManagerWrapper.remove(downloadId);
Log.i(TAG, "Removed the download with Id: " + downloadId);
}
c.close();
}
} catch (Exception e) {
Log.e(TAG, "Exception while removing old downloads.");
}
}
示例8: checkDownloadStatus
import android.app.DownloadManager; //導入依賴的package包/類
public WritableMap checkDownloadStatus(long downloadId) {
DownloadManager.Query downloadQuery = new DownloadManager.Query();
downloadQuery.setFilterById(downloadId);
Cursor cursor = downloadManager.query(downloadQuery);
HashMap<String, String> result = new HashMap<>();
if (cursor.moveToFirst()) {
result = getDownloadStatus(cursor, downloadId);
} else {
result.put("status", "UNKNOWN");
result.put("reason", "COULD_NOT_FIND");
result.put("downloadId", String.valueOf(downloadId));
}
WritableMap wmap = new WritableNativeMap();
for (HashMap.Entry<String, String> entry : result.entrySet()) {
wmap.putString(entry.getKey(), entry.getValue());
}
return wmap;
}
示例9: downloadByDownloadManager
import android.app.DownloadManager; //導入依賴的package包/類
private void downloadByDownloadManager() {
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "支付寶.apk");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("通過DownloadManager下載APK");
request.setDescription("簡單的演示一下DownloadManager的使用方法");
request.allowScanningByMediaScanner();
downloadId = manager.enqueue(request);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id == downloadId) {
ToastUtils.showShortToast("下載完成");
}
}
}, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
示例10: queryChildDocuments
import android.app.DownloadManager; //導入依賴的package包/類
@Override
public Cursor queryChildDocuments(String docId, String[] projection, String sortOrder)
throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
// Delegate to real provider
final long token = Binder.clearCallingIdentity();
Cursor cursor = null;
try {
Query query = new Query();
DownloadManagerUtils.setOnlyIncludeVisibleInDownloadsUi(query);
//query.setOnlyIncludeVisibleInDownloadsUi(true);
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
//query.setFilterByStatus(DownloadManager.STATUS_PENDING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_FAILED);
cursor = mDm.query(query);//.setOnlyIncludeVisibleInDownloadsUi(true)
//.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL));
//copyNotificationUri(result, cursor);
while (cursor.moveToNext()) {
includeDownloadFromCursor(result, cursor);
}
} finally {
IoUtils.closeQuietly(cursor);
Binder.restoreCallingIdentity(token);
}
return result;
}
示例11: doInBackground
import android.app.DownloadManager; //導入依賴的package包/類
@Override
protected Void doInBackground(String... params) {
Uri location = Uri.parse(Utils.BASE_URL_PHP + params[0].substring(1));
String filename = params[0].substring(params[0].lastIndexOf('/') + 1);
DownloadManager downloadManager = (DownloadManager) Utils.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(location);
request.setTitle(filename);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDescription(Utils.getString(R.string.download_description_news));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
request.setDestinationInExternalFilesDir(Utils.getContext(), Environment.DIRECTORY_DOCUMENTS, filename);
else
request.setDestinationInExternalFilesDir(Utils.getContext(), Environment.DIRECTORY_DOWNLOADS, filename);
downloadManager.enqueue(request);
return null;
}
示例12: onReceive
import android.app.DownloadManager; //導入依賴的package包/類
@SuppressLint("NewApi")
public void onReceive(Context context, Intent intent) {
long downLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
long cacheDownLoadId = PreferencesUtils.getLong(context,"download_id");
if(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())){
DownloadManager downloader = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = PreferencesUtils.getLong(context,"download_id");
downloader.remove(downloadId);
//eventbus 關閉dialog
UpdataEvent updataEvent = new UpdataEvent();
updataEvent.setResult("close dialog");
updataEvent.setState(0);
EventBus.getDefault().post(updataEvent);
}else if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction()) && cacheDownLoadId == downLoadId) {
try {
Intent install = new Intent(Intent.ACTION_VIEW);
File apkFile = queryDownloadedApk(context);
install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
}catch (Exception e){
}
}
}
示例13: download
import android.app.DownloadManager; //導入依賴的package包/類
@Override
public DownloadTask download(String url, String filePath) {
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
//設置允許使用的網絡類型,這裏是移動網絡和wifi都可以
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//禁止發出通知,既後台下載,如果要使用這一句必須聲明一個權限:
// android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
//request.setShowRunningNotification(false);
//不顯示下載界麵
request.setVisibleInDownloadsUi(false);
File dstFile = new File(filePath);
request.setDestinationInExternalFilesDir(mContext , dstFile.getParent() , dstFile.getName());
long id = mDownloadManager.enqueue(request);
mDownloadList.add(id);
DownloadTask task = new DownloadTask(id, url , filePath , 0 ,0 , System.currentTimeMillis());
task.setDownloaderType(DownloaderFactory.TYPE_SYSTEM_DOWNLOAD);
return task;
}
示例14: onClickUpdate
import android.app.DownloadManager; //導入依賴的package包/類
/**
* 點擊下載
*/
public void onClickUpdate() {
if (mDownloadId != 0) {//downloadID不為默認值,表示存在下載任務
int status = DownloadUtils.queryDownloadStatus(mContext, mDownloadId);
Log.e("TAG", status + "");
switch (status) {
case DownloadManager.STATUS_RUNNING://下載中
DownloadToast.showShort(mContext, "正在下載,請稍後");
break;
case DownloadManager.STATUS_FAILED://下載失敗
startDownApk();//重新開始下載
break;
case DownloadManager.STATUS_SUCCESSFUL://下載成功
installApk();
break;
default:
break;
}
} else {//無下載任務,開始下載
startDownApk();
}
}
示例15: onCreate
import android.app.DownloadManager; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("clicked");
filter.addAction("download has been paused");
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
this.registerReceiver(receiver, filter);
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.launcher512_qihoo);
pendingIntent = PendingIntent.getBroadcast(
this, 1, new Intent().setAction("clicked"), PendingIntent.FLAG_UPDATE_CURRENT);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}