判断Uri对应的ContentProvider所操作的数据库u存在,及DownloadManager的暂停,继续

   项目中遇到一个问题,就是用到DownloadManager的时候先判断一下,如果有自己定义的用自己的,没有的话用系统的,DownloadManager代码其实写在了工程中,所以一开始不太明白,后来想了想,原来指的是downloadmanager里面的数据库DownloadProvider, 因为DownloadProvider不在我工程的清单文件中,而是在公司的手机中已经早定义好了,而系统的DownloadManager中的DownloadProvider更不是我控制的,

1,知道自己定义的Downloads.Impl.CONTENT_URI;

private void chooseDownloads() {
		Cursor cursor=mContext.getContentResolver().query(uri, null, null, null, null);
		if (cursor == null) {
			isSystemDownload = true;
			systeManager = (android.app.DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
		} else {
			isSystemDownload = false;
			nonSystemManager = DownloadManager.getInstance(mContext);
		}
	}

2. DownloadManager的暂停,
Uri uri=ContentUris.withAppendedId(android.provider.Downloads.Impl.CONTENT_URI, downloadId);
			ContentValues values = new ContentValues();
			values.put(android.provider.Downloads.COLUMN_CONTROL, android.provider.Downloads.Impl.CONTROL_PAUSED);
			values.put(android.provider.Downloads.Impl.COLUMN_STATUS, android.provider.Downloads.Impl.STATUS_PAUSED_BY_APP);
			mContext.getContentResolver().update(uri, values, null, null);

3.DownloadManager继续(必须改变downloadmanager源码,因为系统的downloadmanager不会理会你对COLUMN_STATUS的操作

对downloadmanager源码的修改参考http://www.trinea.cn/android/android-downloadmanager-pro/

Uri uri=ContentUris.withAppendedId(android.provider.Downloads.Impl.CONTENT_URI,downloadId);
			ContentValues values = new ContentValues();
			if (pausedForWifi) {
				values.put(android.provider.Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, android.app.DownloadManager.Request.NETWORK_WIFI | android.app.DownloadManager.Request.NETWORK_MOBILE);
			}
			values.put(android.provider.Downloads.Impl.COLUMN_CONTROL,android.provider.Downloads.Impl.CONTROL_RUN);
			values.put(android.provider.Downloads.Impl.COLUMN_STATUS, android.provider.Downloads.Impl.STATUS_RUNNING);
			mContext.getContentResolver().update(uri, values, null, null);

4.重新开始:

systeManager.restartDownload(downloadId);
			Uri uri=ContentUris.withAppendedId(android.provider.Downloads.Impl.CONTENT_URI, downloadId);
			ContentValues values = new ContentValues();
			values.put(android.provider.Downloads.Impl.COLUMN_CONTROL, android.provider.Downloads.Impl.CONTROL_RUN);
			mContext.getContentResolver().update(uri, values, null, null);

5.判断是否为手动控制pause或run

Uri uri=ContentUris.withAppendedId(android.provider.Downloads.Impl.CONTENT_URI, downloadId);
			Cursor cursor = mContext.getContentResolver().query(uri, null, null,null, null);
			while (cursor.moveToNext()) {
				int control=cursor.getInt(cursor.getColumnIndex(android.provider.Downloads.Impl.COLUMN_CONTROL));
				if (android.provider.Downloads.Impl.CONTROL_RUN!=control&&android.provider.Downloads.Impl.CONTROL_PAUSED != control) {
					return true;
				}
			}
			return false;



你可能感兴趣的:(判断Uri对应的ContentProvider所操作的数据库u存在,及DownloadManager的暂停,继续)