24

Using the function below I am trying to get the LAST url that the user visited from Chrome Browser on their Android Phone. This function works very well for most sites, but does not work for "www.reddit.com".

The url variable below updates if I go to "www.google.com", "www.hulu.com" or "www.kayak.com" but will not update if I go to "www.reddit.com".

It does update if I go to "m.reddit.com". Same problem with facebook, detects m.facebook.com but not www.facebook.com.

I don't understand What's the difference between "www.reddit.com" and "m.reddit.com" that Android records one but not the other.

What change do I need in the code to detect ALL URL ACCESSES?

public String returnLastChromeURL(int browserCode) {
    String[] proj = new String[] { Browser.BookmarkColumns.DATE,
            Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };

    String dateTime;
    Uri uriCustom = Uri
            .parse("content://com.android.chrome.browser/bookmarks");
    String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
                                                            // 1 = bookmark
    try {
    Cursor mCur = mContext.getContentResolver().query(uriCustom, proj, sel,
            null, BookmarkColumns.DATE + " ASC");
    mCur.moveToFirst();
    mCur.moveToLast();

        dateTime = mCur.getString(mCur
                .getColumnIndex(Browser.BookmarkColumns.DATE));
        title = mCur.getString(mCur
                .getColumnIndex(Browser.BookmarkColumns.TITLE));
        url = mCur.getString(mCur
                .getColumnIndex(Browser.BookmarkColumns.URL));

        mCur.close();
    } catch (Exception e) {
        dateTime = String.valueOf(System.currentTimeMillis());
        title = "";
        url = "empty_list";
    }

    return url;
}

Some more info from my debug:

  • When I try this on a Genymotion Emulator, all websites (including sites like www.reddit.com get detected ok). On a real phone www.reddit.com does not get detected.
  • Sites the code will detect OK: www.kayak.com' (redirects towww.kayak.com/mn),www.hulu.com(loads mobile version of site though url stays www.hulu.com),www.google.com` (same story as the hulu).

Seems like that the sites that load the pure desktop version of the site, do not get detected

user1406716
  • 9,565
  • 22
  • 96
  • 151
  • 1
    Have you tried "getAllVisitedUrls" compare with your code? May got some diffrent result? Here are some smaples: http://www.anddev.org/other-coding-problems-f5/getting-recently-visited-urls-from-browser-t4364.html and http://www.programcreek.com/java-api-examples/index.php?api=android.provider.Browser.BookmarkColumns (example 3) Hope I din't give you the wrong way... – VincentBear Apr 29 '15 at 14:25
  • could it be because of the presence of the tags : `` and `` while it is absent in the other pages that you mentioned ? – scraaappy Apr 29 '15 at 17:00
  • @scraaappy - hmmm, good observation but not sure why that would matter. besides even "www.reddit.com" (i.e. desktop version of the site is not getting detected). I tried it on a Genymotion Emulator and all urls (including www.reddit.com) are getting detected, so I am still confused. – user1406716 Apr 29 '15 at 18:28
  • @VincentBear - Thanks, will try out to the samples later today. – user1406716 Apr 29 '15 at 18:28

1 Answers1

8

This is because you never visit the desktop domain on your mobile , actually each time you visit facebook on your mobile ( or any other website that detects your navigator ) it will automatically redirects you to the mobile website , redirections will not be saved in history unless page loads , which never happens.

Check the following screenshot taken from my true N7100 galaxy note2 and my windows browser :

if you are using chrome browser you can request desktop website ( which is not a reliable solution as user will have to do this manually ).

another solution is to override redirection method which will not work due to XSS policy.

so a better solution is to fake your browser and convince the server that you are using a desktop browser check this answer .

The simulator on desktop uses device user agent which will count as desktop browser. screenshot

Community
  • 1
  • 1
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • Thanks but not sure how the answer can be implemented in Android. Any hint here would help. – user1406716 May 01 '15 at 06:08
  • @user1406716 when you go to facebook.com , does your mobile redirect to m.facebook.com ? or are you doing it manually ? – ProllyGeek May 01 '15 at 12:03
  • When I type "facebook.com", chrome goes to "www.facebook.com" desktop site. I have to actually type "m.facebook.com" to go to the mobile site. – user1406716 May 03 '15 at 18:23
  • I have to manually go to `m.facebook.com`, no auto redirection to the mobile site on going to `www.facebook.com` – user1406716 May 11 '15 at 03:30
  • @user1406716 are you browsing using request desktop site turned on ? – ProllyGeek May 11 '15 at 03:53
  • Nope. that setting is **not** turned on. See here: https://www.dropbox.com/s/6cftrlea90twuis/2015-05-11%2003.55.02.png?dl=0 – user1406716 May 11 '15 at 03:58
  • I updated the question (see towards the end) with an observation. Basically, **Seems like that the sites that load the pure desktop version of the site, do not get detected**. Really would like to find a way around this. – user1406716 May 11 '15 at 04:02
  • @user1406716 i think this is a specific issue with your device , because normally when i open any of these sites on my device , i get redirected , are you running a custom rom ? it will be helpful if you send 'about device' screenshot. – ProllyGeek May 12 '15 at 00:42
  • @PrllyGeek - Sorry about the late response, I've been trying a few things. Does this behavior look ok to you? I go to "facebook.com" and Chrome does not redirect to mobile site. I do NOT have hte option to Request Desktop site selected: https://www.youtube.com/watch?v=5ziiyxhICPQ – user1406716 May 31 '15 at 16:32