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.
Related
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.
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
How do i use intent in android widget to open images in browser.
If the image is on the internet it would be as simple as this:
String url = "http://www.picturesnew.com/media/images/image-background.jpg";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
However if the image is inside of your assets you can't open it in the browser. That's because the browser accepts URI data that starts with http. Furthermore the browser is a separate app and one app can't use other apps assets.
You could dipslay the image through the use of a WebView though.
To open a website from your widget:
Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.fakesite.com\articles"));
startActivity(intent);
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
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