2

How to open an html page which is located in my application data in chrome browser? For default browser i am using following code -

Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            browserIntent.setData(Uri.fromFile(file));
             browserIntent.setType("text/html");
            browserIntent.setClassName("com.android.browser",
             "com.android.browser.BrowserActivity");
             browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
             browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         browserIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            startActivity(browserIntent);

But this code does not work on chrome browser. How can i open same html page in chrome browser.

Thanks in advance

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38
  • Remove the line setClassName.. You are specifying a particular activity there which might not even exist on some devices. If you remove that line the OS will present you with the option to choose which browser (or anything else that handles text/html) to view your file with – dymmeh Dec 09 '13 at 17:06
  • But i want to open in chrome without asking user. What class name chrome use so that i will directly put that name at that place. Look at this link - http://stackoverflow.com/questions/12013416/is-there-any-way-in-android-to-force-open-a-link-to-open-in-chrome I used this also,but it doesn't work – unflagged.destination Dec 09 '13 at 18:03
  • try setClassName("com.android.chrome", "com.android.chrome.Main"); Don't forget that if the user does not have chrome installed your intent will crash / fail. This is bad practice. – dymmeh Dec 09 '13 at 18:32
  • I have tried with what u have suggested, but it opens the url in new tab. I want to open the url in same Tab which is already opened. Is it possible? – unflagged.destination Dec 12 '13 at 07:15
  • For opening the link in the same tab, use Browser.EXTRA_APPLICATION_ID: http://stackoverflow.com/a/9918091/1097104 – Juuso Ohtonen Nov 06 '14 at 07:13

1 Answers1

1

You can't specify which browser the user shoud use. Your code is right. Then a little Dialog will be shown to the user where the user can select one of the installed browsers. If you don't get this dialog then you have saved your selection to use it always.

PKeidel
  • 2,559
  • 1
  • 21
  • 29