• 求 在独立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 中的断点



  • 相关阅读:
    性能分析之– JAVA Thread Dump 分析
    定位性能问题的18个linux命令
    jstat命令查看jvm的GC情况
    jstat命令查看tomcat进程提示进程没找到(PID not found
    Linux使用jstat命令查看jvm的GC情况
    jmeter -xml日志格式中网络时间与服务器时间的区分
    性能测试之----瓶颈分析方法
    jmeter dubbo接口测试
    java的 IO流之缓冲流(转载)
    java 中流的使用
  • 原文地址:https://www.cnblogs.com/ondream/p/2328015.html
Copyright © 2020-2023  润新知