can't open url in browser - android

I'm trying to open a url in a web browser using the following code,
does not work
Bundle ex=in.getExtras();
link=ex.getString("link");
public void onClick(View arg0) {
Intent t=new Intent(Intent.ACTION_VIEW);
Uri u=Uri.parse(link);
t.setData(u);
startActivity(t);
}
but i'm havinng a weird problem.When i use the exact url instead of string "link" everything works as it should, but when i go with the String link that i created i get the "no activity found to handle intent" exception.But i'm sure the link contains the right url, the same that i used before.
this works
public void onClick(View arg0) {
Intent t=new Intent(Intent.ACTION_VIEW);
Uri u=Uri.parse("http://google.com");
t.setData(u);
startActivity(t);
}
i'm sure that in the first case link contains the url (http://google.com), besides i printed on a textview to check!

Well, the problem was caused by a whitespace at the of 'link'.I used link.trim() and it worked correctly. Thank you guys for your help.

That is because Intent.ACTION_VIEW acts differently depending on the data you pass with setData.
So if you pass an Uri/URL it will open the browser, but if you pass a String (it doesn't matter that the string actually contains an url) it doesn't know what to do with it.
If you need to create the link from code you can use Uri.withAppendedPath

Related

Web intent url getting modifying automatically

I trying to a webpage link so for this I am using following code
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com/privacy"));
startActivity(browserIntent);
when I click on link, it open in web browser and go to proper page, when i press again on app link and open it again web browser its URL get modifying automatically, on second click i get URL like this
"https://http//example.com/privacy" I don't why this is happening
I find bug in my code
my string file had anchor tag attached thats why new url getting added to intent
this ling had bug
<string name="txt_terms_and_conditions"><a href='https://http://example.com/privacy'>By clicking the Send OTP option,\n you agree to our Terms and Conditions</a></string>
so I removed anchor tag so it working normally

Android: Using Intents to Open a URL without "http"

I am using an intent to open up a specific string literal(the website address) in an Android device's browser.
If I go with
String myIntentString = "url:www.google.com"
Uri myInputUri = Uri.parse(myIntentString);
Intent intent = new Intent(Intent.ACTION_VIEW, myInputUri);
startActivity(intent);
Then the app crashes.
However, if I change my string to "http://www.google.com" (the correct format), then it obviously works.
Yes, this is a school assignment and it is specified that the string MUST be "url:www.google.com"
If it helps, this is the first assignment where we need to turn in the Android Manifest file. Does this mean it has to do with that?
What am I missing?

How can a website owner know that I'm visiting his page using an android app?

I want to create an android app. When I click a row in the ListView, it should go to a website.
In the ListView I'll have my widgets such as TextView, ImageView etc...
For eg: "www.amazon.com"
I tried the following,
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.EMPTY.parse("www.amazon.com"));
context.startActivity(i);
}
});
It successfully redirects to "www.amazon.com".
Here, how will the amazon people know that people are visiting their website using my app?
When you click a link, your browser loads the web page you clicked and tells the website where you came from. For example, if you clicked a link to an outside website on stackoverflow.com, the outside website would see the address of the stackoverflow.com question you came from. This information is contained in the HTTP referrer header.
To add this information you can use the following code, assuming you are using a WebView to display the website
String url = "http://www.amazon.com/";
Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Referer", "http://www.yourwebsite.com");
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.loadUrl(url, extraHeaders);
Using an Intent is not possible with the exception the user would use the default browser which supports this kind of code
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Bundle bundle = new Bundle();
bundle.putString("Referer", "http://www.yourwebsite.com");
browserIntent.putExtra(Browser.EXTRA_HEADERS, bundle);
However if you simply have the WebView in your layout, with visibility invisible, and you do not overwrite the WebClient, i.e.wv.setWebViewClient(new MyWebViewClient());, you get the same behaviour as with the Intent
I've never worked with android apps so I don't know the exact implementation.
But what you could do is setting the header of your webrequest manually so you look like a desktopbrowser. If you're using chrome as a browser you can see what your headers are in the developers tools window under network

Start whatsapp from webview

I have an app that loads a newspaper site in a webview. In that site you can share a news in facebook, twitter and whatsapp. I have facebook's and twitter's links covered with
shouldOverrideUrlLoading()
I can't figer out how to launch the whatsapp app. It uses a custom URL scheme:
whatsapp://send?text=
I get: the webpage at whatsapp://send?text= could not be loaded because:
net::ERR_UNKNOWN_URL_SCHEME
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("tel:") || url.contains("https://wa.me/")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
I suggest using loop from this question: using Intent.ACTION would give You best results.
Don't use wa.me as a domain for sharing texts on whatsapp. Just test this URL here yourself: http://wa.me/?text=mytest. I see:
ERROR
PAGE NOT FOUND
Don't use the wa.me domain. Use the api.whatsapp.com domain. wa.me requires that you use a phone number. Well, typically, you want the share URL to be shared to other people that the user knows, so, you'd want to leave that blank. Check it out...
https://api.whatsapp.com/send?text=YourShareTextHere
https://api.whatsapp.com/send?text=YourShareTextHere&phone=123
Works for me! Hope this helps someone out there!
If you are interested in watching a project that keeps track of these URLs, then check us out!: https://github.com/bradvin/social-share-urls#whatsapp
be sure to have a proper URL encoded string after text=
I've just found that it is possible to open a conversation to a number Using Click to Chat
To create your own link with a pre-filled message that will
automatically appear in the text field of a chat, use
https://wa.me/whatsappphonenumber/?text=urlencodedtext where
whatsappphonenumber is a full phone number in international format and
URL-encodedtext is the URL-encoded pre-filled message.
Example:https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale
NOTE: It opens WhastApp application if you click the link using a mobile phone browser (at least from Android)
this.webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("https://wa.me")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url.replace("+",""))));
return true;
} else {
return false;
}
}
});

Starting a browser from a textbox via Intent - Is http:// required?

I have the following code:
/** Open a browser on the URL specified in the text box */
private void openBrowser() {
Uri uri = Uri.parse(urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
When I input "http://www.google.com" to the textbox, it works fine. However, when I try something like "www.google.com" it crashes with:
No Activity found to handle Intent {
act=android.intent.action.VIEW
dat=www.google.com }
Am I using Uri wrong? Is there a way to extract full address from it? Or am I supposed to write code that adds http manually? e.g, if not starts with http://, add http://.
Thanks!
Am I using Uri wrong?
No. However, www.google.com is not a valid uniform resource identifier.
Or am I supposed to write code that
adds http manually?
Yes.

Categories

Resources