I have a webView and it gets the URL dynamically so I can't edit the HTML document. My problem is that I want the app to detect phone numbers and provide intent but the HTML document does not contain an anchor tag for telephone numbers so I cannot detect the URL. Is there any way to make the WebView identify phone numbers by itself?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith(MAIL_TO)){
final String RE_MAIL = "([\\w\\-]([\\.\\w])+[\\w]+#([\\w\\-]+\\.)+[A-Za-z]{2,4})";
Pattern p = Pattern.compile(RE_MAIL);
Matcher m = p.matcher(url);
while(m.find()) {
sendEmail( m.group(1) );
}
return true;
}else if(url.startsWith(TEL_PREFIX)){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
String appHint = "Choose phone application";
context. startActivity(Intent.createChooser(intent, appHint ));
return true;
}
else{
view.loadUrl(url);
}
return false;
}
Related
I am working on a web Browser, I have a SearchView in it, for user to input queries. I want to differentiate between a search query or a web address. My current code just add http://www. in front of any query that comes in and try to load it.
This is my current code.
String query = search_q;
if(!query.startsWith("www.")&& !query.startsWith("http://")){
query = "www."+ query ;
}
if(!query.startsWith("http://")){
query = "http://"+query;
}
if( Patterns.WEB_URL.matcher(query).matches()){ //checks if the query looks like an URL
web1.loadUrl(query);
}
else
web1.loadUrl("https://www.google.com//search?q=+"+search_q);
The problem is that Patterns.WEB_URL.matcher(query).matches() returns true even if http://www.abc is passed into it.
check
if( Patterns.WEB_URL.matcher(query).matches() && isUrl(query)){ //checks if the query looks like an URL
web1.loadUrl(query);
}
function defintion isUrl:-
public Boolean isUrl(String query){
int a=0;
int onlyfind=0;
for (int i = 0 ; i<query.length() ; i++){
if (query.charAt(i) == '.')
a++;
if(a==1){
onlyfind= i;
}
}
if(a==1){
if(query.substring(0,onlyfind+1).equals("http://www"))
return false;
}
return true;
}
Try validating the url(query) using this method:
//isValidURL(query);
boolean isValidURL(String url) {
try {
new URI(url).parseServerAuthority();
return true;
} catch (URISyntaxException e) {
return false;
}
}
If it returns false, then:
web1.loadUrl("https://www.google.com//search?q=+"+search_q);
I think the best way to validate URL is using regex. Because if you are working on a browser, you should not limit the validation of string queries that starts with www, what if the website is dev.website.com? it is a valid website URL, or what if the address starts with https and not just http?. So I think Try using regex to have a reference pattern in validating if the query string is URL or not:
private boolean isURL(CharSequence searchQuery) {
Pattern urlPattern = Pattern.compile("(https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\w\\D\\S]{2,}|[^\\s]{2,}[a-zA-Z0-9]\\.[^\\s]{2,})");
Matcher matcher = urlPattern.matcher(searchQuery);
return matcher.matches();
}
Then call that method:
String query = search_q;
if(isURL(query)) {
web1.loadUrl(query);
} else {
web1.loadUrl("https://www.google.com//search?q=+"+search_q);
}
Hope this helps.
In my app there is a WebView to which I load a website from server.
There are two cases:
On the loaded website there is a button which should lead to an another app. I know it can be handled like this:
Click me
but this doesn't work in the WebView (only in a standalone browser)! I tried to handle it in shouldOverrideUrlLoading and redirect to an external browser with Intent, but the URI with "intent://" URL is not recognised and cannot be opened.
The link I get from server is the 'intent' link.
The behaviour in both cases should be the same: if app is installed open the app, if not open Google Play do download the app.
Is there any way to do this?
I'm not sure if this is the best option, but I handeled it similarily to what #vineetv suggested. This method is called inside shouldOverrideUrlLoading():
private void handleNewUrl(String url) {
Uri uri = Uri.parse(url);
if (uri.getScheme().equals("http") || uri.getScheme().equals("https"))
openExternalWebsite(url);
else if (uri.getScheme().equals("intent")) {
String appPackage = getAppPackageFromUri(uri);
if (appPackage != null) {
PackageManager manager = getContext().getPackageManager();
Intent appIntent = manager.getLaunchIntentForPackage(appPackage);
if (appIntent != null) {
getActivity().startActivity(appIntent);
} else {
openExternalWebsite("https://play.google.com/store/apps/details?id=" + appPackage);
}
}
}
}
private String getAppPackageFromUri(Uri intentUri) {
Pattern pattern = Pattern.compile("package=(.*?);");
Matcher matcher = pattern.matcher(intentUri.getFragment());
if (matcher.find())
return matcher.group(1);
return null;
}
private void openExternalWebsite(String url) {
Intent webeIntent = new Intent(Intent.ACTION_VIEW);
webeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
webeIntent.setData(Uri.parse(url));
getActivity().startActivity(webeIntent);
}
It seems, to work. But if you have a better solution, let me know!
In my activity class:
final String input = slateURL + OtherHalfUrl;
//here I am combining my Strings to make it a complete Url
//for example slateURL = "http://example.com/id=" & OtherHalfUrl = 69
//So String input becomes like this "http://example.com/id=69"
Log.e("Complete URL", input);//in log I am checking and it is correct Url
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl(input);//no output
// webView.loadUrl("http://example.com/id=69");//getting output
what is the reason for this or am I Doing something wrong.
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
OtherHalfurl I am getting from my other activity fragment like this:
Intent intent = new Intent(getContext(), GoToActivity.class);
intent.putExtra("id",idno);
And getting it like this :
String OtherHalfUrl = getIntent().getStringExtra("id");
Try this
webView.loadUrl(YOUR URL);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I am loading string value in my web view and i need when i click on the link shown in web view should open in other web browser of phone.
I want like this - String s = "hello read more.... www.google.com"
String html = "<html><body>+s+</body></html>"
webview.loaddata(html);
Try this:
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
You can do something like this:
String s = "<a href='http://www.google.com' target='_blank'>Read more over here</a>";
webview.loaddata(s);
You will need to add other html content by yourself.
So basically, you can use target='_blank' which will open it in a new browser, not in webview.
Hope it helps.
[SOLVED, but I'm open to new suggestions...]
I'm integrating Twitter into my Android app using twitter4j.
When I try to authorize with Twitter, I am calling the following endpoint with my oauth token:
https://api.twitter.com/oauth/authenticate?oauth_token=MY_VALID_TOKEN
which should redirect me to:
MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
but instead, it redirects me to:
https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***
which is obviously not a valid url.
(Also, the : is missing - it should be MY-CALLBACK:///...)
Please note I'm using WebView for my calls
I could manipulate this string to make everything work, but there has to be a better way...
I am passing my callback URL to
getOAuthRequestToken("MY-CALLBACK:///");
and have already set the intent-filter for my activity with
<data android:scheme="x-oauthflow-twitter" />
Also, the activity has android:launchMode="singleInstance"
What am I doing wrong?
[edit:more details]
mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(Constants.TWITTER_CONSUMER_KEY, Constants.TWITTER_CONSUMER_SECRET);
twitterWebView = new WebView(ActivityTwitterAuthorize.this);
twitterWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(Constants.TWITTER_CALLBACK_URL)) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
// HACKY PART!
// I added the following code to force it to work, but this is a dirty hack...
// String TWITTER_CALLBACK_INVALID_PREFIX = "https://api.twitter.comx-oauthflow-twitter///";
// TWITTER_CALLBACK_URL = "MY-CALLBACK:///";
// BEGIN
} else if (url.startsWith(TWITTER_CALLBACK_INVALID_PREFIX)) {
url = url.substring(TWITTER_CALLBACK_INVALID_PREFIX.length());
url = Constants.TWITTER_CALLBACK_URL + url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
// END
} else {
view.loadUrl(url);
}
return true;
}
});
mTwitterReqToken = mTwitter.getOAuthRequestToken(Constants.TWITTER_CALLBACK_URL);
twitterWebView.loadUrl(mTwitterReqToken.getAuthenticationURL());
WITHOUT the hacky part, this code results in "Webpage not available" error, because the url is invalid:
https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***
If the url was MY-CALLBACK:///?oauth_token=***&oauth_verifier=*** then my activity would receive an Intent, and everything would be ok...
WITH the "hacky part", my code works, but I would like to avoid that piece of code.
I found I just could not get it to work this way after following the guides I've seen online.
I ended up using my own custom WebViewClient with the code:
if ( url.contains( "MY-CALLBACK:///" ) )
{
final int start = url.indexOf( '?' ) + 1;
final String params = url.substring( start );
final String verifierToken = "oauth_verifier=";
if ( params.contains( verifierToken ) )
{
final int value = params.indexOf( verifierToken ) + verifierToken.length();
final String token = params.substring( value );
view.stopLoading();
authoriseNewUser( token );
}
else if ( params.contains( "denied" ) )
{
view.stopLoading();
finish();
}
}
else
{
view.loadUrl( url );
}
return true;
Use Below CallBack_URI for that, it may help you.
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String OAUTH_CALLBACK_HOST = "callback";
public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
I guess there is nothing wrong with your code. I was getting the same result yesterday, but today it works like a charm. It is probably a server side issue. Could you try again your original (with no hacky part) solution, pls?
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String OAUTH_CALLBACK_HOST = "litestcalback";
public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME+ "://" +OAUTH_CALLBACK_HOST;
use this type of callback_url in code and manifest file...