下面列出了android.app.DownloadManager.Request#setMimeType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void downloadPlugin(PluginDownloadHolder dataSource) {
Request request = new DownloadManager.Request(
dataSource.getDownloadLink());
request.setDestinationInExternalFilesDir(
GeoARApplication.applicationContext, null,
dataSource.getIdentifier() + ".apk");
request.setTitle("GeoAR Data Souce Download");
request.setMimeType("application/vnd.52north.datasources");
request.setDescription(dataSource.getName());
currentDownloads.add(mDownloadManager.enqueue(request));
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private static DownloadInfo add(Builder b) {
Context context = b.mContext;
removeAllForUrl(context, b.mUrl);
if (!b.mDialog) {
synchronized (mCallbacks) {
mCallbacks.put(b.mUrl, b.mCallback);
}
}
String savePath = "Download/EdXposedManager";
if (b.mModule) {
savePath += "/modules";
}
Request request = new Request(Uri.parse(b.mUrl));
request.setTitle(b.mTitle);
request.setMimeType(b.mMimeType.toString());
if (b.mSave) {
try {
request.setDestinationInExternalPublicDir(savePath, b.mTitle + b.mMimeType.getExtension());
} catch (IllegalStateException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else if (b.mDestination != null) {
b.mDestination.getParentFile().mkdirs();
removeAllForLocalFile(context, b.mDestination);
request.setDestinationUri(Uri.fromFile(b.mDestination));
}
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = dm.enqueue(request);
if (b.mDialog) {
showDownloadDialog(b, id);
}
return getById(context, id);
}
/**
* download map
*
* @param view View
* @param position item position
*/
private void onClickMapNow(int position)
{
MyMap myMap = myDownloadAdapter.getItem(position);
if (myMap.getStatus() == MyMap.DlStatus.Downloading || myMap.getStatus() == MyMap.DlStatus.Unzipping)
{
logUser("Already downloading!");
return;
}
else if (myMap.isUpdateAvailable())
{
MyMap myMapNew = null;
if (!myMap.getMapNameNew().isEmpty())
{
int removePos = -1;
for (int i= 0; i<myDownloadAdapter.getItemCount(); i++)
{
if (myDownloadAdapter.getItem(i).getMapName().equals(myMap.getMapName()))
{
removePos = i;
}
if (myDownloadAdapter.getItem(i).getMapName().equals(myMap.getMapNameNew()))
{
position = i;
myMapNew = myDownloadAdapter.getItem(i);
}
}
if (removePos < 0 || myMapNew == null)
{
logUser("OldMap or NewMap missing on json-list!");
return;
}
mapsRV.scrollToPosition(position);
myDownloadAdapter.remove(removePos);
if (position > removePos) { position--; }
}
MainActivity.clearLocalMap(myMap);
if (myMapNew != null)
{
myMap = myMapNew;
if (myMap.getStatus() != DlStatus.On_server)
{
logUser("New map is: " + myMap.getMapName());
return;
}
}
}
else if (myMap.getStatus() == MyMap.DlStatus.Complete)
{
logUser("Already downloaded!");
return;
}
myMap.setStatus(MyMap.DlStatus.Downloading);
myDownloadAdapter.refreshMapView(myMap);
String vers = "?v=unknown";
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
try
{
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
vers = "?v=" + packageInfo.versionName;
}
catch (Exception e) {} // No problem, not important.
Request request = new Request(Uri.parse(myMap.getUrl() + vers));
File destFile = MyMap.getMapFile(myMap, MyMap.MapFileType.DlMapFile);
request.setDestinationUri(Uri.fromFile(destFile));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setMimeType("application/pocketmaps");
long enqueueId = dm.enqueue(request);
File idFile = MyMap.getMapFile(myMap, MyMap.MapFileType.DlIdFile);
IO.writeToFile("" + enqueueId, idFile, false);
BroadcastReceiver br = createBroadcastReceiver(this, createStatusUpdater(), myMap, enqueueId);
registerReceiver(br, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
receiverList.add(br);
}
@Override
public Void run(JobContext jc) {
try {
if (checkDownloadRunning())
return null;
if (checkApkExist()) {
Intent installApkIntent = new Intent();
installApkIntent.setAction(Intent.ACTION_VIEW);
installApkIntent.setDataAndType(
Uri.parse(Preferences.getDownloadPath(mContext)),
"application/vnd.android.package-archive");
installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(installApkIntent);
} else {
String apkName = mContext.getPackageName()
+ System.currentTimeMillis() + Constants.APK_SUFFIX;
// 系统下载程序
final DownloadManager downloadManager = (DownloadManager) mContext
.getSystemService(mContext.DOWNLOAD_SERVICE);
Long recommendedMaxBytes = DownloadManager
.getRecommendedMaxBytesOverMobile(mContext);
// 可以在移动网络下下载
if (recommendedMaxBytes == null
|| recommendedMaxBytes.longValue() > MAX_ALLOWED_DOWNLOAD_BYTES_BY_MOBILE) {
allowMobileDownload = true;
}
Uri uri = Uri.parse(mUpgradeInfo.getUrl());
final Request request = new Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
int NETWORK_TYPE = DownloadManager.Request.NETWORK_WIFI;
if (allowMobileDownload) {
NETWORK_TYPE |= DownloadManager.Request.NETWORK_MOBILE;
}
request.setAllowedNetworkTypes(NETWORK_TYPE);
request.allowScanningByMediaScanner();
request.setShowRunningNotification(true);
request.setVisibleInDownloadsUi(true);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, apkName);
PackageManager packageManager = mContext.getPackageManager();
ApplicationInfo applicationInfo = packageManager
.getApplicationInfo(mContext.getPackageName(), 0);
Log.i("liweiping",
"appName = "
+ packageManager
.getApplicationLabel(applicationInfo));
request.setTitle(packageManager
.getApplicationLabel(applicationInfo));
request.setMimeType("application/vnd.android.package-archive");
// id 保存起来跟之后的广播接收器作对比
long id = downloadManager.enqueue(request);
long oldId = Preferences.getDownloadId(mContext);
if (oldId != -1) {
downloadManager.remove(oldId);
}
Preferences.removeAll(mContext);
Preferences.setDownloadId(mContext, id);
Preferences.setUpgradeInfo(mContext, mUpgradeInfo);
Preferences.setDownloadStatus(mContext,
Constants.DOWNLOAD_STATUS_RUNNING);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public Void run(JobContext jc) {
try {
if (checkDownloadRunning())
return null;
if (checkApkExist()) {
Intent installApkIntent = new Intent();
installApkIntent.setAction(Intent.ACTION_VIEW);
installApkIntent.setDataAndType(
Uri.parse(Preferences.getDownloadPath(mContext)),
"application/vnd.android.package-archive");
installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(installApkIntent);
} else {
String apkName = mContext.getPackageName()
+ System.currentTimeMillis() + Constants.APK_SUFFIX;
// 系统下载程序
final DownloadManager downloadManager = (DownloadManager) mContext
.getSystemService(mContext.DOWNLOAD_SERVICE);
Long recommendedMaxBytes = DownloadManager
.getRecommendedMaxBytesOverMobile(mContext);
// 可以在移动网络下下载
if (recommendedMaxBytes == null
|| recommendedMaxBytes.longValue() > MAX_ALLOWED_DOWNLOAD_BYTES_BY_MOBILE) {
allowMobileDownload = true;
}
Uri uri = Uri.parse(mUpgradeInfo.getUrl());
final Request request = new Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
int NETWORK_TYPE = DownloadManager.Request.NETWORK_WIFI;
if (allowMobileDownload) {
NETWORK_TYPE |= DownloadManager.Request.NETWORK_MOBILE;
}
request.setAllowedNetworkTypes(NETWORK_TYPE);
request.allowScanningByMediaScanner();
request.setShowRunningNotification(true);
request.setVisibleInDownloadsUi(true);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, apkName);
PackageManager packageManager = mContext.getPackageManager();
ApplicationInfo applicationInfo = packageManager
.getApplicationInfo(mContext.getPackageName(), 0);
Log.i("liweiping",
"appName = "
+ packageManager
.getApplicationLabel(applicationInfo));
request.setTitle(packageManager
.getApplicationLabel(applicationInfo));
request.setMimeType("application/vnd.android.package-archive");
// id 保存起来跟之后的广播接收器作对比
long id = downloadManager.enqueue(request);
long oldId = Preferences.getDownloadId(mContext);
if (oldId != -1) {
downloadManager.remove(oldId);
}
Preferences.removeAll(mContext);
Preferences.setDownloadId(mContext, id);
Preferences.setUpgradeInfo(mContext, mUpgradeInfo);
Preferences.setDownloadStatus(mContext,
Constants.DOWNLOAD_STATUS_RUNNING);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}