2

当我提出另一个活动的意图时,我有一个活动正在被换出。onPause 调用 saveState() 来保存到目前为止的工作:

private void saveState() {
    ...
    ...
    if (myUri == null) {
        // Inserting a new record
*** myUri = getContentResolver().insert(ContentProvider.CONTENT_URI, values);
    } else {
        // Update an existing record
        getContentResolver().update(myUri, values, null, null);
    }
}

在调用 getContentResolver() 之前,ContentProvider.CONTENT_URI = 'content://nz.co.bkd.extraTime.contentprovider/times'。

调用后,myUri = 'times/#' 其中#=row ID。我的问题是;返回的 uri 的 'content:...' 前缀在哪里?

在调用过程中,ContentResolver.java 被调用并返回 CreatedRow uri

ContentResolver.java
....
....
public final Uri insert(Uri url, ContentValues values)
{
    IContentProvider provider = acquireProvider(url);
    if (provider == null) {
        throw new IllegalArgumentException("Unknown URL " + url);
    }
    try {
        long startTime = SystemClock.uptimeMillis();
***     Uri createdRow = provider.insert(url, values);
        long durationMillis = SystemClock.uptimeMillis() - startTime;
        maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
        return createdRow;
    } catch (RemoteException e) {
        // Arbitrary and not worth documenting, as Activity
        // Manager will kill this process shortly anyway.
        return null;
    } finally {
        releaseProvider(provider);
    }
}

此时,createdRow = 'times/#'。

记录确实保存在 Sqlite 数据库中。

我必须在我的代码中添加 uri 前缀还是应该返回完整的 uri?

4

0 回答 0