5

我的应用程序有一项活动。该应用程序有一个抽屉,里面有一个从我的内容提供者那里填写的列表。用户可以从抽屉中选择一个项目,然后活动将动态填充适当的内容。我不确定在这种情况下如何实现应用索引。我的意思是根据本教程的第 3 步,该活动似乎应该显示一个内容(我错了吗)?

注意:我已经进行了深度链接(我有一个网站,并且内容映射到应用程序中的内容)。

具体来说,我想知道每次用户更改内容时我都会动态更改以下内容:

    mUrl = "http://examplepetstore.com/dogs/standard-poodle";
    mTitle = "Standard Poodle";
    mDescription = "The Standard Poodle stands at least 18 inches at the withers";

如果是的话,我应该只打一次电话(仅在 onStart 中)。同样,我的数据是从内容提供商加载的。提供者本身是从服务器加载的,但该调用会加载所有内容——而不是仅加载单个页面。

4

1 回答 1

3

AFAIK,您应该GoogleApiClient只为每个活动连接一次。但是,您可以根据需要尽可能多地索引动态内容(但最好不要索引内容太多次),只需记住在活动完成时断开它们。以下是我在项目中所做的:

HashMap<String, Action> indexedActions;
HashMap<String, Boolean> indexedStatuses;
public void startIndexing(String mTitle, String mDescription, String id) {
    if (TextUtils.isEmpty(mTitle) || TextUtils.isEmpty(mDescription))
        return; // dont index if there's no keyword
    if (indexedActions.containsKey(id)) return; // dont try to re-indexing
    if (mClient != null && mClient.isConnected()) {
        Action action = getAction(mTitle, mDescription, id);
        AppIndex.AppIndexApi.start(mClient, action);
        indexedActions.put(id, action);
        indexedStatuses.put(id, true);
        LogUtils.e("indexed: " + mTitle + ", id: " + id);
    } else {
        LogUtils.e("Client is connect : " + mClient.isConnected());
    }
}

public void endIndexing(String id) {
    // dont endindex if it's not indexed
    if (indexedStatuses.get(id)) {
        return;
    }
    if (mClient != null && mClient.isConnected()) {
        Action action = indexedActions.get(id);
        if (action == null) return;
        AppIndex.AppIndexApi.end(mClient, action);
        indexedStatuses.put(id, false);
    }
}
于 2016-06-16T17:08:05.397 回答