内容提供商uris从一个应用程序到其他访问

时间:2014-10-01 06:28:44

标签: android android-contentprovider

我已经从一个应用创建了一个内容uri:

content://SendDataProvider/bookevent

我可以将值插入其中。 喜欢:

content://SendDataProvider/bookevent/1
content://SendDataProvider/bookevent/2 etc...

但是当我将其调用到其他应用程序以获取此URI中的所有记录时,我使用

String bookContentDataLog = "content://SendDataProvider/bookevent";
Uri bookrecord = Uri.parse(bookContentDataLog);
Cursor cursor = BookShelfActivity.this.getContentResolver().query(
                    bookrecord, null, null, null, null);

if (cursor.moveToFirst()) {  // I got Null pointer exception here
                String sessionId = cursor.getString(cursor.getColumnIndex(BOOK_COLUMN_SESSIONID));
                String magId = cursor.getString(cursor.getColumnIndex(BOOK_COLUMN_MAG_ID));
}

我调试它,我发现

BookShelfActivity.this.getContentResolver().query(
                    bookrecord, null, null, null, null);

这会将null值返回到游标。

我如何将所有值插入到此内容中:// SendDataProvider / bookevent"?

==============我用used方法插入uri =================== 1)类名和变量

public class SendDataProvider extends ContentProvider {


public SendDataProvider(Context c) {
        this.context = c;//getContext();
        BookDb dbHelper = new BookDb(context);
        db = dbHelper.getWritableDatabase();
    }

2)内容提供者类methode:===

@Override
    public Uri insert(Uri uri, ContentValues contentValues) throws NullPointerException {
        Log.e("SendDataProvider @Override inserted call","Uri insert");
        long rowID = db.insert(BOOK_TABLE_NAME, null, contentValues);
        Log.e("SendDataProvider @Override inserted ID","Uri insert ID"+rowID);
        if (rowID > 0) {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
            this.context.getContentResolver().notifyChange(_uri, null);
            return _uri;
        }
        throw new SQLException("Failed to add a record into " + uri);
    }

3)查询

@Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {

              SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
              qb.setTables(BOOK_TABLE_NAME);

              switch (uriMatcher.match(uri)) {
              case uriCode:
               qb.setProjectionMap(this.values);
               break;
              default:
               throw new IllegalArgumentException("Unknown URI " + uri);
              }
              if (sortOrder == null || sortOrder == "") {
               sortOrder = BOOK_COLUMN_MAG_ID;
              }
              Cursor c = qb.query(db, projection, selection, selectionArgs, null,
                null, sortOrder);
              c.setNotificationUri(this.context.getContentResolver(), uri);
              return c;
    }

1 个答案:

答案 0 :(得分:1)

求助:

第1步: 使用DB inser / update / delte / query创建Provider类,并注意这些事情。

static final String PROVIDER_NAME = "com.example.contentprovideruser.DataProvider";//"org.geometerplus.android.fbreader.interfaces.SendDataProvider";
static final String URL = "content://" + PROVIDER_NAME + "/bookevent";
static final Uri CONTENT_URI = Uri.parse(URL);

您需要创建好的URI

第2步:添加 PROVIDER_NAME AndroidManifest.xml

 <provider android:name=".DataProvider" 
       android:authorities="com.example.contentprovideruser.DataProvider"
       android:exported="true"
       android:multiprocess="true">
 </provider>

第3步:需要指定好的MIME TYPE

@Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case BOOK:
            return "vnd.android.cursor.dir/vnd.booktype";
        case BOOK_ID:
            return "vnd.android.cursor.item/vnd.booktype";
        default:
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
    }

第4步:“CONTENT_URI”是您插入所有值的全局uri

您可以使用查询来获得结果。

Uri students = DataProvider.CONTENT_URI;
          Cursor c = getContentResolver().query(students, null, null, null, "name");
          if (c.moveToFirst()) {
             do{
                Toast.makeText(this, 
                c.getString(c.getColumnIndex(DataProvider.BOOK_COLUMN_EVENT_NAME)) + 
                ", " + c.getString(c.getColumnIndex( DataProvider.BOOK_COLUMN_FROMPAGE_NO)), 
                Toast.LENGTH_SHORT).show();
             } while (c.moveToNext());
          }

高兴:)