I am getting an issue while opening URL in my default browser (Chrome)
My requirement is opening URL in the last open tab in Chrome browser for 4.4.4 (Chrome is default browser). NOT in a new tab.
Below code is opening URL in a new tab:
Intent mBrowserIntent = new Intent(Intent.ACTION_VIEW);
mBrowserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); mBrowserIntent.putExtra(Browser.EXTRA_APPLICATION_ID,mContext.getPackageName()); mBrowserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
mBrowserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBrowserIntent.setData(Uri.parse(mUrl));
mContext.startActivity(mBrowserIntent);
Please help in resolving this issue.
Thanks,
This works for me, note that you should not use getPackageName():
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://yahoo.com"));
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.browser");
startActivty(intent);
EDIT: I just tested it on 4.2.2, and it works.
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.
I'm doing Android application which I want to open an HTML file from my application data on Chrome browser without manual interrupt. Currently it's working fine for default browser.But I want to open it on chrome browser.
My Code for default browser:
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);
How could I proceed that on Chrome browser explicitly.If I tried like setClassName("com.android.chrome", "com.android.chrome.Main"), I'm getting exception as
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW..." .
Please help me to get out this issue. Is there any other way?Thanks.
Set classname as
com.android.chrome/com.google.android.apps.chrome.Main
browserIntent.setClassName("com.android.browser", "com.android.chrome/com.google.android.apps.chrome.Main");
Like a parental control application, I have bloacked few websites. As the blocked url is detected its re-directed to about:blank.
Evey thing is working fine, problem arises in Android 4.4 Chrome browser.
about:blank opens in new tab, along with recentaly open tabs.
I want the close all thoes tab. Below is what i have done.
Intent mBrowserIntent = new Intent(Intent.ACTION_VIEW);
mBrowserIntent.setPackage(packageName);
mBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
mBrowserIntent.putExtra(Browser.EXTRA_APPLICATION_ID , packageName);
Uri uri = Uri.parse(defaultURL);
mBrowserIntent.setData(uri);
startActivity(mBrowserIntent);
Expected Result
To open a webpage in external built-in browser (not popup window) when clicking a link in a web view.
Problem
When Popup Browser is enabled in Samsung Galaxy Note II (GT-N7100), the web page is opened in the Popup Browser, and I don't want this. I want the web page to be opened in external built-in browser even if the Popup Browser is enabled.
The setting of Popup Browser in the Samsung Note II can be found at Settings -> Application manager -> ALL -> Popup Browser.
IDE
Android version = 4.1.1
Device = Samsung Galaxy Note II (GT-N7100)
Code
public class PeppermintWebViewClient extends WebViewClient {
//....
private void handleOpenBrowserScheme(String url){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(url);
intent.setData(uri);
mainActivityInstance.startActivity(intent);
}
}
I don't understand your problem clearly but may this help you:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse((String) i.getExtras().get("url")));
startActivity(browserIntent);
where "url" is the URL to which you want to go in your browser.
And i is an INTENT that holds the "url" from another activity.
Can this help you?
I'm encountering a strange error when I try to open a local HTML - file in the android browser. The error that occurs is an activity not found exception:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml?
What's really strange to me ist the fact that this error does not occur on all devices. It only comes up on the following devices:
Samsung Galaxy Nexus I (4.1.1)
Samsung Galaxy Nexus II (4.1.1)
Sony Tablet S (4.0.3)
I have tested my Code successfully on the following devices:
Samsung Galaxy S I (4.1.1)
Samsung Galaxy S III
HTC One X
HTC Desire S
Samsung Galaxy S Plus (2.3.3)
Samsung Galaxy Tab 10.1N
AVDs with Android Versions ranging from 2.3.3 to 4.1.0
Finally this is the Code I'm using to open my HTML - file with the Android - browser. I have tested several permutations of this code, leaving out some lines, but they do all have the same effect. As soon as I set component or class I getting the above exception.
Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
I have also tried
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
instead of
browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
But with the same effect...
Has someone an idea why this is happening, am I missing something? I have searched for days on this but couldn't find anything that solved my problem...
Thanks a lot in advance,
cheers
Possible because may be there is no any activity like com.android.browser.BrowserActivity in those devices, Its depends on device manufacturer How they implement the native Browser Application (Activity Name and Package name).
So the possible solution is,
Using PackageManger and Intent you can check for specific intent category like, Intent.CATEGORY_BROWSABLE is available for any application if available then set that application to ComponentName.
Or, You don't specify component name, like,
browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
let user have to choose, which Activity will open this page,
So just code is,
Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
in some scenario you might want to use the native browser instead of letting user chose one,
this is how I did to avoid ActivityNotFoundException by different package name, hope this would help :)
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
PackageManager packageManager = currentActivity.getPackageManager();
Uri uri = Uri.parse(url);
browserIntent.setDataAndType(uri, "text/html");
List<ResolveInfo> list = packageManager.queryIntentActivities(browserIntent, 0);
for (ResolveInfo resolveInfo : list) {
String activityName = resolveInfo.activityInfo.name;
if (activityName.contains("BrowserActivity")) {
browserIntent =
packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName);
ComponentName comp =
new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
browserIntent.setAction(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setComponent(comp);
browserIntent.setData(uri);
}
}
Why not leave it to the system to determine which app to use?
If I have (for example) Chrome installed and perfer to use Chrome, I'll be a bit pissed that you forced the default browser on me.
This should work:
final Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
Why are you specifying a component? leave it outside of your Intent and you should be fine; the action, category and data/type are enough.
New devices can come with Chrome instead of native browser, so you should use for this
browserIntent.setComponent(new ComponentName("com.android.chrome", "com.google.android.apps.chrome.IntentDispatcher"));