求 在独立service 中 调用contentprovider的方法

求 在独立service 中 调用contentprovider的方法

已用方法,调用会出错

       <provider 
android:authorities="com.bu3GZ.provider.Download"
android:name=".DBContentProvider" >
</provider>
package Insper.bu3GZ.Download;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

/*
* 提供外部访问任务信息
* 线程信息直接调用DownloadDBHelper
*/
public class DBContentProvider extends ContentProvider {
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private DownloadDBHelper mOpenHelper;
//private Context ct;
static {
sUriMatcher.addURI("com.bu3GZ.provider.Download",
"download_taskRecord", 1);
sUriMatcher.addURI("com.bu3GZ.provider.Download",
"download_taskRecord/#", 2);
}

//public void DBContentProvider(Context context) {
//android.os.Debug.waitForDebugger();
//mOpenHelper = new DownloadDBHelper(context, null, null, 0);// 使用默认值
//this.ct = context;
//}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
if (!checkUri(uri))
return 0;
int e;
e = this.mOpenHelper.wParamSQLiteDatabase.delete("download_taskRecord",
selection, selectionArgs);
if (e == 0)
getContext().getContentResolver().notifyChange(uri, null);
return 0;
}

@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
return "com.bu3GZ.provider.Download";
}

@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
if (!checkUri(uri))
return uri;
for (ContentValues cv = new ContentValues(values);; cv = new ContentValues()) {
if (-1 == mOpenHelper.wParamSQLiteDatabase.insert(
"download_taskRecord", null, values))
throw new SQLException("Failed to insert row into " + uri);
getContext().getContentResolver().notifyChange(uri, null);
}
}

@Override
public boolean onCreate() {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
this.mOpenHelper = new DownloadDBHelper(this.getContext(), "", null, 1);
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
if (!checkUri(uri))
return null;
return mOpenHelper.wParamSQLiteDatabase.query("download_taskRecord",
projection, selection, selectionArgs, null, null, sortOrder);

}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
android.os.Debug.waitForDebugger();
if (!checkUri(uri))
return 0;
if (mOpenHelper.wParamSQLiteDatabase.update("download_taskRecord",
values, selection, selectionArgs) <= 0)
return 0;
else
getContext().getContentResolver().notifyChange(uri, null);
return 0;
}

/*
* 检查是否是本地操作
*
* @param 内容操作链接
*/
private boolean checkUri(Uri uri) {
switch (sUriMatcher.match(uri)) {
case 1:
break;
case 2:
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
return true;
}
}

在外部用会出错

                ContentResolver cr = getContentResolver();
Uri uri = Uri
.parse("content://com.bu3GZ.provider.Download/download_taskRecord");
cr.acquireContentProviderClient(uri);
String URL = cr.getType(uri);
cr.query(uri, new String[]{"file_name"}, null, null, null);

gettype 就会出错 在service中的contentprovider 设置断点不进入 包含oncreate 中的断点



原文地址:https://www.cnblogs.com/ondream/p/2328015.html