Launch Browser Intent with Custom Class - cannot find Activity - android

I want to specifically run the default Android browser for a given URL. I'm using this code:
Intent i = new Intent();
i.setAction("android.intent.action.VIEW");
i.addCategory("android.intent.category.BROWSABLE");
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);
The error I receive is:
Unable to find explicit activity class {
com.google.android.browser/com.android.browser.BrowserActivity};
have you declared this activity in your AndroidManifest.xml?
I also tried filtering the intents by the package:
i.setPackage("com.google.android.browser");
instead of setClassName, but to no avail:
No Activity found to handle Intent { act=android.intent.action.VIEW
cat=[android.intent.category.BROWSABLE]
dat=http://www.google.com/ flg=0x10000000 pkg=android }
I also tried adding <uses-library android:name="com.google.android.browser" /> to the manifest.
Am I missing something here?
PS: I'm not interested in using startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))) as it will list all the choices for the browsing Intent.

i use this, it's ok.
intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i think you know what wrong. :)

Please note the default browser can be overridden and it's not always the built in browser app, it can be for example Opera Mini.
You need to do this way:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.google.com");
intent.setData(data);
startActivity(intent);

One way to open an URL in a browser from code is to use a webview .
Create a WebViewClientClass that extends the WebViewClient like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Then create a webview.
Set webview.setWebViewClient(new WebViewClientClass()); --- this is a small workaround so that the default web browser doesn't take over .
Then take the url in an edittext and set it to load the browser as :
webview.loadurl(urlfield.getText().toString());
webview.requestFocus();
This should load the web browser with the URL that you requested.
Hope this helps... :)

Related

Is there a way to open a browser tab automatically in Android?

I'm looking for a way to open a browser at a specific link automatically at a give time / daily.
Maybe there is some kind of script, add-on for bowser but I still haven't find one.
Does anyone know a good solution?
I suggest you to use an AalarmManager here is an example
And then here is the code you should execute
String urlString = "your_url";
Intent intent = new
Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException ex) {
context.startActivity(intent);
}
Seems to me this is a combination of three things, setting the alarm, firing the intent and making sure the intent has the data to open a specific uri in the browser.
start activity from an alarm
open a uri

Why would an intent to open an external browser window not work, and instead open within the web view?

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?

Launch Chrome from Android app but point to local content

So, from my app I can launch Chrome and get it to load a page using the http:// using the code below.
My problem is that when I try and load content that is stored locally on the device. i.e. file:///, I get the error further down.
Any ideas on how to work around this?
Code:
String urlString="file:///storage/emulated/0/Download/primer.html";
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
startActivity(intent);
Error:
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.VIEW
dat=file:///storage/emulated/0/Download/cuescript_primer.html
flg=0x10000000 pkg=com.android.chrome }

Android launching specific url with specific browser

Launching a specific browser by icon is done with a ACTION_MAIN.
Launching a specific url using default browser is done with a ACTION_VIEW.
What if you want to open a specific url in a specific browser?
If you know the package name and the class name of the browser,you can use
Intent.setClassName (String packageName, String className). looks like:
Intent i=new Intent(ACTION_VIEW, url);
i.setClassName("com.test.browser","BrowserActivity");
startActivity(i);
You can even call the specific browser via its package name.
Like this;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.co.com"));
intent.setPackage("org.mozilla.firefox");
startActivity(intent);
I think this is a setting. You go to the Application Manager and go to your favorite Browser. Then at the Options it should show you to be your default.
I am not an Android Developer by the way...

Open url in browser and force page reload

I use the following code to open an URL in browser:
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
startActivity(intent);
But if the page behind the url was already opened in browser, running the above code doesn't refresh the page. How could I force page refreshing?
Looking for an answer as well... this seems to work for me:
Uri url = Uri.parse(website+"?time="+(new Date().getTime()));
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(url);
activity.startActivity(i);
because the new get parameter forces a reload.
to my knowledge you cannot force it to refresh from the Android side (unless you're showing it in your own WebView) You'd have to set the page itself to auto refresh, that would presumably ensure whatever is being shown is likely to be up to date.

Categories

Resources