I can open my local html file with android browser by following way:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(webPageUri, "text/html");
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);
And it works. But I would like to open my local html file in default browser without specifing:
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
Is there a way to to that?
Edit:
If I remove setClassName as you suggest, it opens in HtmlViewer (it is not a default browser).
And if I do it like that:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(webPageUri);
startActivity(intent);
I get: ActivityNotFoundException
Just use:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(webPageUri, "text/html");
startActivity(intent);
This will give the user a list of installed browsers to choose from. If there is only one browser, then that's the one that will be launched.
Just remove the setClassName() line and you are set to go.
This will launch the default browser if it is the only browser in the phone. If there are more than one, the user will have to select one.
Related
I am developing an android application. I am currently opening a new activity with WebView when the user click on a URL. I want to change it load the URL inside the the browser. How is that possible?
Simply do like this.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
I have an Android SDK that uses a web view to show some pages, but depending on the requested link, I have it open an external browser. I am using the WebView's shouldOverrideUrlLoading method, and if it's a link to be opened externally, I use:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
This works for us in a demo app, but someone integrating our SDK says that it's opening within the web view. I don't have access to their code, but what is the developer able to do on his side to prevent the default action to open an external browser? I have thought of two possible solutions that I found on SO:
Use some flags to make sure no existing tasks are used, like so:
Intent i = new Intent(Intent.ACTION_VIEW);
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
i.putExtras(b);
i.addCategory(Intent.CATEGORY_BROWSABLE);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setData(Uri.parse(url));
startActivity(i);
Would this be sufficient?
Another solution is to force opening in the chrome browser, like so:
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
context.startActivity(intent);
What would be the preferable solution? Or am I missing anything else?
I have a String url. The requirement : On Click of a Button, the default chooser should open if the deeplinked App is installed with the User else opens the browser.
I am doing this onClick of the Button:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url);
startActivity(i);
The onClick opens the browser only, evenif the deeplinked App is installed. What should be the approach?
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url);
startActivity(i);
The above code worked in case of deep-linked URL. The default chooser gave option for the App.
I'm trying to open market app from my app using:
Intent i = new Intent("Intent.ACTION_VIEW");
i.setData(Uri.parse("market://details?id=com.compamy.app"));
startActivity(i);
But I have No Activity Found Error. I am using real device and I have market installed.
So my question is: What can be done here?Thanks.
The only problem here is what zapl pointed out in the comments ("" around the action). The documentation (http://developer.android.com/guide/publishing/publishing.html) clearly states how to use this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
official documantation link:
http://developer.android.com/guide/publishing/publishing.html#marketintent
Opening the app details page from your Android app
To open the Google Play details page from your application, create an intent with the ACTION_VIEW action and include a data URI in this format:
market://details?id=
For example, here's how you can create an intent and open an application's details page in Google Play:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
Try this:
Uri uri = Uri.parse("market://details?id=com.compamy.app");
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
I'm loosing my mind over this. I want to open the user's default web browser. I can use this:
startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse("http://google.com")));
To open the browser and send the user to that URL. But I don't want to send him to a specific URL, I just want to open the browser. I'm sure it's a simple solution, I just can't find it. Any Ideas?
To just open the browser without any URL opened you can use
startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse("about:blank")));
After a bunch of searching, I was able to do this:
PackageManager pm = getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setClassName(af.packageName, af.name);
startActivity(launchIntent);
It basically says "What application would handle this?". Then it grabs that applications package and class name then fires an intent for the main action.