How can I open a web page from an Android app? - android

I am trying to open a web page from my Android app.
This is what I did:
successCallback = {
url-> runOnUiThread{
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("$url"))
startActivity(browserIntent)}
}
But it doesn't work. Any thoughts?
This the error message

Please check your URL is correct or not i.e URL contain proper link or value, then parse it.
successCallback = {
url-> runOnUiThread{
if (url!= null && !url.isEmpty()){
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("$url"))
startActivity(browserIntent)}}

successCallback = {
url-> runOnUiThread {
Intent browserIntent = new Intent (Intent.ACTION_VIEW,
Uri.parse ("YOUR URL") );
startActivity (browserIntent);
}

String url = yoururl;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Do you want to open the page within your app? You should use a Webview or chrome custom tabs https://developer.android.com/guide/webapps/webview

Related

Android - Open URL from json results

I have a question about URL got from JSON details.
This is my code:
JSONObject currentEarthquake = couponCategoryArray.getJSONObject(i);
JSONObject properties = currentEarthquake.getJSONObject("campaign");
String name = properties.getString("name");
String promo_code = currentEarthquake.getString("promocode");
String goto_store = currentEarthquake.getString("goto_link");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
CouponCategory couponCategory = new CouponCategory(name, promo_code, goto_store);
couponcategory.add(couponCategory);
From JSON, I get 3 fields: name, promocode and goto_link.
goto_link is URL.
My intent is click the link on goto_link field to open the link in the browser.
I added an intent under goto_link String.
Some suggestion to code it correctly?
Add this.
Your URI starts with HTTP or HTTPS like this: http://www.google.com
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
startActivity(browserIntent);
public void openWebPage(String url) {
try {
Uri webpage = Uri.parse(url);
Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request. Please install a web browser or check your URL.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
How is you goto link formatted ?
anyways an answer from #MarkB solves it
string goto_link = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_link));
startActivity(browserIntent);
incase your link doesnt have the http:// part
string goto_link = "www.google.com";
if (!goto_link.startsWith("http://") && !goto_link.startsWith("https://"))
goto_link = "http://" + goto_link;
You can open a link in the browser like this:
yourButton.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(goto_store)
if (intent.resolveActivity(requireContext().packageManager) != null)
startActivity(intent)
}

Open PDF in browser

What I want to achieve is to display PDFs directly in browser.
private void openPDF(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
With this method the PDF is downloaded, but I have to manually open it in order to see it.
I remember this method worked just fine a while a go, did something change in the intent or chrome maybe?
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
If this can help.

Open a URL in Android's web browser from application

How can I open a url in the android web browser from my application on clicking a button.
I tried this :
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
but I am getting exception:
but I got an Exception :
"No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"
You are doing it fundamentally correct, you just need to include the full url.
String strURL="http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https, if you want a secure connection over SSL.
Almost seems like this would be useful to read... from the page:
Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);
There is a second way, involving the WebView, but that seems like it's not your question. Hope this helped.
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Try this:
String strURL = "http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;

Facebook Link doesnt work android

Hello I just want to call Facebook app with the link below (on my android project):
String url_facebook_prixo = "https://www.facebook.com/pages/Prixo/468580313168290";
I tried this :
Uri uri = Uri.parse("facebook://facebook.com/page{468580313168290}");
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(viewIntent);
I tried with others link but its only displaying my wall..
Someone?
You should use the id to open the page.
Intent facebookIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("fb://profile/468580313168290"));
startActivity(facebookIntent);
I would advice you to encapsulate this in a try catch. Inside the catch open a normal browser intent

Starting navigation in Waze with Android's Intents

I have a simple Android app which should be able to allow navigation between 2 GeoPoint's.
I can easily display a GeoPoint on Waze, writing this small piece of code:
String uri = "waze://?ll=40.761043, -73.980545&z=10";
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
But, what I really need is a way to display navigation directions between 2 points.
I've tried to locate the correct BroadcastReciever in the Waze source, but I stopped follow when it got to native calls (JNI) because I have no idea where the actual call is... I reached only to the URIHandler signature, with no success finding the implementation...
Any ideas?
Thanks.
Fix you uri to:
String uri = "waze://?ll=40.761043, -73.980545&navigate=yes";
(added navigate=yes) and you should be good.
If this is still relevant you can use this:
String uri = "geo: latitude,longtitude";
startActivity(new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(uri)));
Today's Waze base url is https://waze.com/ul so to navigate you have to use
https://waze.com/ul?ll=45.6906304,-120.810983&navigate=yes
You can do [kotlin]:
fun openWaze(latitude: Double, longitude: Double) {
packageManager?.let {
val url = "waze://?ll=$latitude,$longitude&navigate=yes"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
intent.resolveActivity(it)?.let {
startActivity(intent)
} ?: run {
Toast.makeText(context, R.string.noWazeAppFound, Toast.LENGTH_SHORT).show()
}
}
}
According to Google doc about working with Waze you can use code below to open waze from your android app:
try {
// Launch Waze to look for Hawaii:
String url = "https://waze.com/ul?ll=40.761043,-73.980545&navigate=yes";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException ex) {
// If Waze is not installed, open it in Google Play:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.waze"));
startActivity(intent);
}

Categories

Resources