为什么在我的设备上测试时看不到我以编程方式创建的书签

发布于 2024-12-27 19:10:04 字数 3738 浏览 0 评论 0原文

我有一个应用程序可以显示所有书签。我可以列出书签,然后我的按钮添加书签。我尚未实现立即更新列表的代码,但当我返回时,新书签将显示在列表中。 它是我要添加的默认书签,我添加了

 map.put(Browser.BookmarkColumns.CREATED, creationTime);
                map.put(Browser.BookmarkColumns.TITLE, addTitle);
                map.put(Browser.BookmarkColumns.BOOKMARK, 1);
                cr.update(Browser.BOOKMARKS_URI, map,"_id = " + cur.getInt(0), null);

新书签显示在列表中的这些字段,但是当我打开浏览器并查看书签选项卡时,所有其他书签都存在,但不是我创建的任何书签。

我已经在版本 7 和 10 的模拟器上对此进行了测试,它可以正常工作,但是当我在运行版本 2.3.3 的三星 Galaxy s2 上尝试它时,它会添加到列表中。我已经在设备上单步执行并创建了书签,我知道这一点,因为我的代码设置为在找不到时添加,或者在找到时重写。

我安装了不止一种浏览器 我的设备上有 Firefox 和 Opera。 Firefox 保留自己的书签,我还没有使用过 Opera,

这是所有添加书签的代码,

private void addBookmark(String addTitle, String addUrl)    {
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(BOOKMARKS_URI, projection,     android.provider.Browser.BookmarkColumns.BOOKMARK, null, null);
    //first find out if this title has been used before
    boolean found = false;
    long creationTime = new Date().getTime();
    if (cur.moveToFirst()) {
        String title;           
        int count = 0;
        int titleColumn;

        do {                 
            titleColumn = cur.getColumnIndex(BookmarkColumns.TITLE);
            title = cur.getString(titleColumn);
            if (title.equals(addTitle)) 
            {
                found = true;
                ContentValues map = new ContentValues();
                map.put(Browser.BookmarkColumns.CREATED, creationTime);
                map.put(Browser.BookmarkColumns.TITLE, addTitle);
                map.put(Browser.BookmarkColumns.BOOKMARK, 1);
                map.put(Browser.BookmarkColumns.DATE, 0);
                map.put(Browser.BookmarkColumns.VISITS, 0);
                cr.update(Browser.BOOKMARKS_URI, map,"_id = " + cur.getInt(0), null);
                Log.d(LOGTAG,"title found: "+ Integer.toString(count));
            }
            count++;   
        }while (cur.moveToNext());
    }else {
        Log.d(LOGTAG,"didn't move to first");
    }

    if (!found) {

            cur = cr.query(BOOKMARKS_URI,
                    projection, 
                    android.provider.Browser.BookmarkColumns.BOOKMARK, 
                    null, 
                    null);
            // Adding a bookmark for a site the user has not been to.
            ContentValues map = new ContentValues();
            map.put(Browser.BookmarkColumns.TITLE, addTitle);
            map.put(Browser.BookmarkColumns.URL, addUrl);
            map.put(Browser.BookmarkColumns.CREATED, creationTime);
            map.put(Browser.BookmarkColumns.BOOKMARK, 1);
            map.put(Browser.BookmarkColumns.DATE, 0);
            map.put(Browser.BookmarkColumns.VISITS, 0);
            cr.insert(Browser.BOOKMARKS_URI, map);
    }
}

这是我尝试的最后一个代码 我将访问值设置为 100,现在它显示为历史记录项,但有一颗星将其显示为收藏项。这对我来说,我将这些值之一设置错误或遗漏了一个。历史记录选项卡知道它是最喜欢的,但最喜欢的选项卡不知道

if (!found) {
        cur = cr.query(BOOKMARKS_URI,
                projection,
                android.provider.Browser.BookmarkColumns.BOOKMARK,
                null,
                null);
        //Adding a bookmark for a site the user has not been to.
        ContentValues map = new ContentValues();
        map.put(Browser.BookmarkColumns.TITLE, "Random title");
        map.put(Browser.BookmarkColumns.URL, "http://www.bbc.co.uk");
        map.put(Browser.BookmarkColumns.CREATED, System.currentTimeMillis());
        map.put(Browser.BookmarkColumns.BOOKMARK, 1);
        map.put(Browser.BookmarkColumns.DATE, 0L);
        map.put(Browser.BookmarkColumns.VISITS, 100);
        getContentResolver().insert(Browser.BOOKMARKS_URI, map);


    }

I have an app which shows me all of the bookmarks. I can list the bookmarks then my button adds a bookmark. I have not implemented the code to update the list right away but when I return the new bookmark is showing in the list.
Its the default bookmarks I'm adding to and I add these fields

 map.put(Browser.BookmarkColumns.CREATED, creationTime);
                map.put(Browser.BookmarkColumns.TITLE, addTitle);
                map.put(Browser.BookmarkColumns.BOOKMARK, 1);
                cr.update(Browser.BOOKMARKS_URI, map,"_id = " + cur.getInt(0), null);

the new bookmark shows in the list but when I open the browser and view the bookmarks tab all of the other bookmarks are present but not any that I have created.

I have tested this on emulators at version 7 and 10 and it works without issue but when I try it on my samsung galaxy s2 running on version 2.3.3 it adds to the list. I have stepped through on the device and the bookmark is created I know this because my code is set to add if it can't find or just rewrite if it does find.

I have more than one browser installed I've got firefox and opera on my device. Firefox keeps its own bookmarks and I haven't used opera yet

here's all the add bookmark code

private void addBookmark(String addTitle, String addUrl)    {
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(BOOKMARKS_URI, projection,     android.provider.Browser.BookmarkColumns.BOOKMARK, null, null);
    //first find out if this title has been used before
    boolean found = false;
    long creationTime = new Date().getTime();
    if (cur.moveToFirst()) {
        String title;           
        int count = 0;
        int titleColumn;

        do {                 
            titleColumn = cur.getColumnIndex(BookmarkColumns.TITLE);
            title = cur.getString(titleColumn);
            if (title.equals(addTitle)) 
            {
                found = true;
                ContentValues map = new ContentValues();
                map.put(Browser.BookmarkColumns.CREATED, creationTime);
                map.put(Browser.BookmarkColumns.TITLE, addTitle);
                map.put(Browser.BookmarkColumns.BOOKMARK, 1);
                map.put(Browser.BookmarkColumns.DATE, 0);
                map.put(Browser.BookmarkColumns.VISITS, 0);
                cr.update(Browser.BOOKMARKS_URI, map,"_id = " + cur.getInt(0), null);
                Log.d(LOGTAG,"title found: "+ Integer.toString(count));
            }
            count++;   
        }while (cur.moveToNext());
    }else {
        Log.d(LOGTAG,"didn't move to first");
    }

    if (!found) {

            cur = cr.query(BOOKMARKS_URI,
                    projection, 
                    android.provider.Browser.BookmarkColumns.BOOKMARK, 
                    null, 
                    null);
            // Adding a bookmark for a site the user has not been to.
            ContentValues map = new ContentValues();
            map.put(Browser.BookmarkColumns.TITLE, addTitle);
            map.put(Browser.BookmarkColumns.URL, addUrl);
            map.put(Browser.BookmarkColumns.CREATED, creationTime);
            map.put(Browser.BookmarkColumns.BOOKMARK, 1);
            map.put(Browser.BookmarkColumns.DATE, 0);
            map.put(Browser.BookmarkColumns.VISITS, 0);
            cr.insert(Browser.BOOKMARKS_URI, map);
    }
}

this was the last code I tried
I set the visits value to 100 and now its showing as a history item but with a star showing it as a favourite. This says to me I'm setting one of these values wrong or missing one out. The history tab knows its a favourite but the favourite tab doesn't

if (!found) {
        cur = cr.query(BOOKMARKS_URI,
                projection,
                android.provider.Browser.BookmarkColumns.BOOKMARK,
                null,
                null);
        //Adding a bookmark for a site the user has not been to.
        ContentValues map = new ContentValues();
        map.put(Browser.BookmarkColumns.TITLE, "Random title");
        map.put(Browser.BookmarkColumns.URL, "http://www.bbc.co.uk");
        map.put(Browser.BookmarkColumns.CREATED, System.currentTimeMillis());
        map.put(Browser.BookmarkColumns.BOOKMARK, 1);
        map.put(Browser.BookmarkColumns.DATE, 0L);
        map.put(Browser.BookmarkColumns.VISITS, 100);
        getContentResolver().insert(Browser.BOOKMARKS_URI, map);


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2025-01-03 19:10:04

编辑:鉴于您的代码更新,请尝试以下操作:

private static final String SELECTION = BookmarkColumns.TITLE + "=? AND " +
    BookmarkColumns.URL + "=? AND " + BookmarkColumns.BOOKMARK + "=1";

private void addBookmark(String title, String url) {
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(Browser.BOOKMARKS_URI, new String[]{BaseColumns._ID}, SELECTION, new String[]{title, url}, null);
    if (cursor != null) {
        try {
            if (!cursor.moveToNext()) {
                // No such bookmark, create a new instance
                ContentValues values = new ContentValues();
                values.put(BookmarkColumns.BOOKMARK, 1);
                values.put(BookmarkColumns.CREATED, System.currentTimeMillis());
                values.put(BookmarkColumns.DATE, 0L);
                values.put(BookmarkColumns.TITLE, title);
                values.put(BookmarkColumns.URL, url);
                values.put(BookmarkColumns.VISITS, 0);
                cr.insert(Browser.BOOKMARKS_URI, values);
            }
        } finally {
            cursor.close();
        }
    }
}

Edit: Given your code update, try something like this instead:

private static final String SELECTION = BookmarkColumns.TITLE + "=? AND " +
    BookmarkColumns.URL + "=? AND " + BookmarkColumns.BOOKMARK + "=1";

private void addBookmark(String title, String url) {
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(Browser.BOOKMARKS_URI, new String[]{BaseColumns._ID}, SELECTION, new String[]{title, url}, null);
    if (cursor != null) {
        try {
            if (!cursor.moveToNext()) {
                // No such bookmark, create a new instance
                ContentValues values = new ContentValues();
                values.put(BookmarkColumns.BOOKMARK, 1);
                values.put(BookmarkColumns.CREATED, System.currentTimeMillis());
                values.put(BookmarkColumns.DATE, 0L);
                values.put(BookmarkColumns.TITLE, title);
                values.put(BookmarkColumns.URL, url);
                values.put(BookmarkColumns.VISITS, 0);
                cr.insert(Browser.BOOKMARKS_URI, values);
            }
        } finally {
            cursor.close();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文