Launch custom Android application from WebView - android

I have an HTML file which is launching an app if I open it in the Android native browser,
but when I try to open the same in a WebView, it is not able to launch that application, and "Webpage not available" is shown. I think my WebView is not able to handle the scheme "my.special.scheme://" defined for the application.
I read Launching an Android Application from the Browser, but it does not cover information about launching an app from a WebView.

It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.
What you need to do is add a custom WebViewClient to your WebView:
webView.setWebViewClient(new CustomWebViewClient());
and then in the shouldOverrideUrlLoading(), have the following code:
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
if (url.startsWith("my.special.scheme://")) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// The following flags launch the app outside the current app
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
return true;
}
return false;
}

I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.
The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.

I perform an ACTION_VIEW with the URL to make the URL open in the default browser, which will do the redirecting to the concerning app (I had to fix this concerning payments via a bank app)

Related

issues with Android app with screen going back to Top upon clicking adding items to basket

I created an APP for a restaurant using a there website in Andriod studio but have two issues
On the order page on the APP when I try to add item to basket screen scrolls back to top without adding item to basket. (when I tried to open the website on mobile it works fine on mobile browser i.e user can keep on adding items to basket)
when I select menu on website it directs me to PDF file which doesn't work when I used webview, I also tried using Intent but that didn't help
Please help and many thanks in Advance
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
view.getSettings().setJavaScriptEnabled(true);
startActivity(intent);
return true;
this will open pdf in Browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
this will open pdf in webview use either one
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);

How to open the Amazon AppStore directly from my Android application?

I have seen answers regarding Amazon Appstore from iPhone but not from Android. I'm trying to make a button that will open the Amazon Appstore (for android) on my app's page.
Here is how it's done for Google Play:
final String appPackageName = context.getPackageName();
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (android.content.ActivityNotFoundException e) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
How should I replace the Strings so that it works "the same" for the Amazon AppStore?
The Amazon app store URI is
amzn://apps/android?
Directing to a particular app would look like
amzn://apps/android?p=com.amazon.mp3
Source: Linking To the Amazon Appstore for Android
Look into WebViews. It does exactly what you want, you can open a page in your own app. Simply define a webview in the xml, and use the java to display the page.
How to do it without webview:
(From docs)
By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored. If your goal is
only to display some HTML as a part of your UI, this is probably fine;
the user won't need to interact with the web page beyond reading it,
and the web page won't need to interact with the user. If you actually
want a full-blown web browser, then you probably want to invoke the
Browser application with a URL Intent rather than show it with a
WebView. For example:
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
With webview:
webview.loadUrl("http://slashdot.org/");
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Let me know if this works by commenting.

Android: 3d-secure redirect response

I have an Android app where I am processing payments within the app. The payment also requires 3d-secure verification sometimes. So this requires redirecting the user to a webpage where they will be able to make some appropriate actions: Such as entering a code or such. In my case, the app is targeted towards Swedish users and it redirects them to a page where they must open another "bank ID" app, either on the same device or another, to perform this verification.
On our iOS app this feature works as expected. Once the user has performed the verification, the browser receives a callback which can then be used to update the app accordingly, but on Android, the WebView I am using is not notified. So I am unable, so far, to handle the user-verification event.
Does anybody have experience with this or any similar use-case? Any help is appreciated.
We have experienced a similar issue with Nordea's 3D Secure page in an Android WebView.
It came down to the page trying to access local storage. We added the code below to the app to get it to work:
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mWebView.getSettings().setDatabasePath("/data/data/" +
mWebView.getContext().getPackageName() + "/databases/");
}
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("intent:")){
Intent intent = new Intent();
intent.setPackage("com.bankid.bus");
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("bankid");
intent.setData(Uri.parse("bankid://www.bankid.com?redirect=null")) ;
startActivityForResult(intent, 0);
return true;
}
// your existing override code goes here probably "return false"
// to stop webview redirects to browser.
}
});
mWebView.loadUrl(url);

Opening Facebook app with another ? With the "Twitter way"

My app needs to display a Facebook account. So I would like the Facebook application opens or if it is not available, the web browser. Currently I can open the Twitter app with this way and the system ask what to do (Twitter app or the browser).
Here is the code:
WebView myWebView = (WebView) findViewById(R.id.webViewProfile); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://mobile.twitter.com/"+name); // Specify the URL to load when the application starts
myWebView.setInitialScale(1); // Set the initial zoom scale
myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control
myWebView.getSettings().setUserAgentString("BumpMe");
This method is very good and I want to have the same result for Facebook. Currently I can open the Facebook app like that:
String uri = "facebook://facebook.com/info?user="+fbid;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
But if the application is not installed it won't work...
How can I do it like for twitter ?
Thanks
The reason this works with Twitter is that the Twitter app handles https://mobile.twitter.com/* URL's in its intent filters; so when you issue an intent that has that URL, the Twitter app gets called. So the only way to do this with Facebook (or any other app) would be to use a URL that matches one of the patterns declared in its intent filters. Maybe http://m.facebook.com/... ?
Instead of offering the user the choice of app or browser, another way is to try to open the app, and if that fails to open the web page. Here is how I do that for Twitter:
long userID = tweet.getUser().getId();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=" + userID));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/user?user_id=" + userID)));
}

Android - Call default browser with and redirect to a designated url

Hi
I want to write an app to call default browser and redirect to a designated url.
Any suggestion to 1)call the default browser, 2)redirect to a designated url.
Thanks
you just want to launch an ACTION_VIEW intent with the Uri of the webpage as your data element :
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("http://www.bbc.co.uk"));
startActivity(httpIntent);
To open the default browser use an Intent with the action VIEW. To tell the browser which page to load us the data-part of the Intent.
Example:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(browse);
Since this is a basic task in Android you might want to read some basics about Intents in Android.

Categories

Resources