How to open links in Android app in external browser? - android

Can anyone help me in Code for opening links in external browsers or other Android app?
Now the case is the link is opening in the app itself. But if the link belongs to an android app its not opening. It's showing install the Android app.
So I want that if the link can be opened in browsers, then it will ask from a list of browsers. Or if the links belongs to an app it must show the app in the list too.

Something like this could work
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(browserIntent);

As #zain posted ago you can use.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent);
But if you have more then one browser installed in device and want to choose from one of them. Use intent chooser like this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
// Always use string resources for UI text. This says something like "Share this photo with"
String title = getResources().getText(R.string.chooser_title);
// Create and start the chooser
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);
refer from here
Show browser list when opening a link in android

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/"));
String title = "Complete Action Using";
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);

If you are in a WebView in your App, and on clivking a link there, if the app opens the link in the App Itself, Then possibly u should have overridden this method.
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//view.loadUrl(url);
return false;
}
});
The return of false should ask the user, where to Open the link. With the browsers installed in the mobile

Related

Is there a way to open a link in a browser window?

I currently use this
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://www.google.com"));
startActivity(intent);
It currently opens in my default browser.
But I need it to open like this.
Is there any simple way to do that?
If you want to open any specific browser application rather then opening the url on default browser. Set component to your implicit intent for an example if you want to open the URL only on chrome application then you can call it like this:
String url = "https://www.google.com";
try {
Intent i = new Intent("android.intent.action.MAIN");
i.setComponent("com.android.chrome/com.android.chrome.Main");
i.addCategory("android.intent.category.LAUNCHER");
i.setData(Uri.parse(url));
startActivity(i);
} catch(ActivityNotFoundException e) {
// Google chrome application is not installed in user's phone
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
Sometimes it may happen that application is not available on user's phone in that case catch the exception and open the default browser.

Android app opening browser in the same tab?

I'm finding this hard to explain, but i'll do my best.
I have an app that opens the browser (firefox) and send information from that app to the webpage as a php variable, the problem i have is that i am doing this quite a lot, and everytime it opens the browser its opening a new tab.
I know there is no way to close a tab using javascript etc, so is there a way to ensure it always opens into the same current tab so i dont end up with several open at once.
I dont want to keep having to close firefox tabs whenever the app fires up the browser.
Sorry if its hard to make sense of.
Thanks.
Probably this is a bit late, but I have managed to accomplish what you want only using Chrome app.
String url = "http://www.mypage.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
This allows you to re-use the current opened tab on Chrome and change the current URL in that tab to the one you are providing in the intent.
I also have tried with no success on Firefox.
I hope this helps.
For Firefox android app to replace the existing url of current tab.
String package = "org.mozilla.firefox";
String url = "http://www.test.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage(package);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, package);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
For chrome or other browsers, just change the package name. Hopefully it will solve the problem.
Answer used to open a "sample.html" file in same tab instead of creating a new tab of Google Chrome browser.
btnClickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory(), "sample.html");
Uri uri2 = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW, uri2);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.Main");
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
try {
startActivity(intent);
} catch (Exception e) {
Log.e("Exception ", e.getLocalizedMessage());
}
}
});
Note : Kindly check your Storage permission before proceeding.

Show browser list when opening a link in android

I want to open some links in an external browser. Before opening, I want to display the list of browsers on the device. I did this functionality using Intent.ACTION_VIEW. But as per my app's requirement, I want to display the list of browsers even if there is only one browser application on the device. Does anyone have any idea about this? Thanks.
If you have one browser application , the chooser will not launched , the URL will be loaded on that browser application. If you have two or more browser applications, the android system launch an IntentChooser the show the list of all browser apps installed on the device , so the user can choose his preferred browser to load the URL :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent);
EDIT :
to create your custom Intent Chooser :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
// Always use string resources for UI text. This says something like "Share this photo with"
String title = getString(R.string.chooser_title);
// Create and start the chooser
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);
refer this
Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.maidofknowledge.com"));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
settings>apps>clear default (
this is if you have previously set "use as default app" for chrome
these 2 images are my source:
Message to select browser or select default:
http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-03.jpg
Clear default browser, show intent chooser again:
http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-02.jpg

Open page in Facebook,Twitter and Google Plus app from other app - Android

I'm working on an application where I need to integrate the social functionality of the different social networks: Facebook, Twitter, Google+.
For now, in Facebook and Twitter i'm recognized if the user has a native application and if he does, I'm opening it and show him my fan page.
For Twitter I use the next code:
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/[user_name]")));
}
And for Facebook the next code:
try{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + PROFILE_FACEBOOK_APP_ID));
startActivity(intent);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/UserNamePage")));
}
Now I want to do the same thing for Google+. I saw that I can browse to my fan page with the next Url https://plus.google.com/MY_PAGE_ID/, but it keep asking me if I want to open it with Google+ application or with the browser, and I want that he will open it with the application automatically, without asking the user.
Is there a simple way to do this?
Thanks.
Found a solution:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus",
"com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", "FAN_PAGE_ID");
startActivity(intent);
I think this is quite safe, because we do not need to specify the component, just the google+ app package name:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://plus.google.com/[Google+ID]/"));
intent.setPackage("com.google.android.apps.plus"); // don't open the browser, make sure it opens in Google+ app
startActivity(intent);
Unknown if google plus needs some other information in the Intent but as general Android solution you can explicitly set the target. You will need the package name of google+.
More info here: http://developer.android.com/reference/android/content/Intent.html#setPackage%28java.lang.String%29
For example:
Intent.setPackage("com.google.android.apps.plus"); //Don't know the exact package name

Android Link to Market from inside another app

I am building many apps for Android and wish to have a menu button in the apps that basically opens a list of my other apps in the Android Market.
Is there a way to create an intent and have the Android market pop up with a search (of my company) in the market so users can buy other apps?
ian
Even better to use "market://details" instead of "market://search":
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.android.example"));
startActivity(intent);
Then it opens directly the details page of the app. With search it shows a single search result and the user has to do an additional click to get to the detail page.
Yes, there is a documented Intent syntax for that (http://market.android.com/search?q=pub:<Developer Name> or market://search?q=pub:<Developer Name>).
The intent action would be view, and uri the market url/uri.
Like this:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pub:<developer name>") ) );
Another way is to launch a URL Intent with your application's package name in it.
User will get popup with list of installed Browsers + Play Store app in which he can view your target app.
String appPackageName = "com.example.android";
String url = "https://play.google.com/store/apps/details?id=" + appPackageName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
Above code is tested and works as expected on Play Store version 4.1.6
On my real devices Sony Xperia Pro and PocketBook tablet, even when you put a link to play web store e.g. https://play.google.com/store/apps/details?id=com.estrongs.android.pop
It will ask if you want to open it in the default browser or in the Play market.
If you select Play market - application is shown as expected.
Did not test it with intent, tested with Autolink from TextView.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.adfree:
final String appPackageName = "com.zooohooo.noads"; // Can also use getPackageName(), as below
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
return true;
case R.id.rate:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
return true;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources