求 在独立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/archive/2012/01/20/2328015.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值