Parameter passed from WebView to Activity - android

I have to call web page from WebView to Activity.This is a two way communication which I want to achieve between an Activity and a page loaded in WebView.
Edit
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent); return true;
}
}

Extend the WebViewCLient (example from tutorial):
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Have a look: http://developer.android.com/guide/webapps/webview.html
Just create an activity with a webview inside and load the URL.

Related

How do I load url from Bundle in Android

I cannot load page using link that is passed from a previous Activity.
The code is as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_webpage);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
/*Bundle b = getIntent().getExtras();
String url = b.getString(DeviceDetails.URL_KEY);*/
String url = getIntent().getStringExtra(DeviceDetails.URL_KEY);
//String url = "http://google.pl/nexus/4";
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
So, when I use String url="http://google.pl/nexus/4" all seems to be fine. And I'm totally sure that my activity gets the url from getIntent because I debugged it.
UPD1:
String inputUrl = detUrlEditText.getText().toString();
Intent intent = new Intent(DeviceDetails.this, ShowWebPageActivity.class);
Bundle extras = new Bundle();
extras.putString(URL_KEY, inputUrl);
intent.putExtras(extras);
startActivity(intent);
Previous Activity. It is guaranteed to pass url because I debugged it. And toast also shows passed url in ShowWebPageActivity.
Don't call view.loadUrl(url) inside the method shouldOverrideUrlLoading().
I've seen examples elsewhere that do this and I don't understand why, this shouldn't be necessary.
shouldOverrideUrlLoading() should return true when you are handing the URL and the WebView should not load it, and return false when the WebView should proceed with loading the URL.
You could call view.loadUrl() if you decide that the WebView should open a different page than the URL parameter, for example.
Here's an example of a WebViewClient subclass:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (handleWithSystemBrowser(url)) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
return true; // tells WebView that we specifically handled the URL, so don't load it
}
return false; // go ahead and load it in the WebView
}
private boolean handleWithSystemBrowser(String url) {
// put your code here to check the URL
// return true if you want the device browser to display the URL
// return false if you want the WebView to load the URL
.
.
.
}
}

webview loadUrl opens the url in browser

I am trying to open the url in webview, but it is opening the url in external browser.
My code is
public class MainActivity extends Activity
{
private WebView webView;
String URL = "https://login.salesforce.com/services/oauth2/authorize?response_type=token&display=touch&client_id=3MVG9Y6d_Btp4xp7w.6oGLerlRztTTUKMEL8QWHvuB5miUgdJzQ0HgnMB7dhm1mTiluRv4ud9cZYeFcAz7hXP&redirect_uri=https%3A%2F%2Flogin.salesforce.com%2Fservices%2Foauth2%2Fsuccess";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl(URL);
//webView.loadUrl("https://www.google.co.in/");
webView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {
// This is your web site, so do not override; let the WebView to load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
// this will ignore the Ssl error and will go forward to your site
handler.proceed();
}
}
}
If the url is https:www.google.com then the url is opening in webview only. I have googled hard to find the solution but I am unable to find it. How to resolve the issue?
The issue is in your
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {
// This is your web site, so do not override; let the WebView to load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
its trying to load the URL as a different intent, hence your default browser kicks in.
what do you really want to check with
Uri.parse(url).getHost().equals(url)
try putting logs for the check you will figure out what wrong.

Android: WebView - open certain URLs inside WebView, the rest externally?

Here's my code:
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.shufflemylife.com/shuffle");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("/shuffle")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
return true;
}
}
}
Basically, I want any url containing '/shuffle' to load inside WebView, and anything else to be opened in the external browser. Is it doable? How close am I do accomplishing this?
Thanks for any help!
To clarify, this is how you should write your shouldOverrideUrlLoading method:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("/shuffle")) {
mWebView.loadUrl(url);
return false;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
}
Is it doable?
Yes.
How close am I do accomplishing this?
Close, but backwards. The default behavior of a WebView is to display links in the external browser. Hence, if url.contains("/shuffle"), you want to call loadUrl() on your WebView to keep the link internal, and return true in that case. If this is a URL you want handled normally, return false.

Android WebView setting download folder location

I've added a webview activity and would like to control which folder any downloaded that my end users will download via my app. The files will be pdf files and I would like them to go into a specific folder that my app uses rather than the default download folder.
public class myweb extends Activity {
private WebView myWebView;
private String LOG_TAG = "AndroidWebViewActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.knitweb);
myWebView = (WebView) findViewById(R.id.webView1);
//enable Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
//loads the WebView completely zoomed out
myWebView.getSettings().setLoadWithOverviewMode(true);
//true makes the Webview have a normal viewport such as a normal desktop browser
//when false the webview will have a viewport constrained to it's own dimensions
myWebView.getSettings().setUseWideViewPort(true);
//override the web client to open all links in the same webview
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebChromeClient(new MyWebChromeClient());
//Injects the supplied Java object into this WebView. The object is injected into the
//JavaScript context of the main frame, using the supplied name. This allows the
//Java object's public methods to be accessed from JavaScript.
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
//load the home page URL
//myWebView.loadUrl("http://demo.mysamplecode.com/Servlets_JSP/pages/androidWebView.jsp");
myWebView.loadUrl("http://www.patonsyarns.com/pattern.php?PID=4521&cps=21191");
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("demo.mysamplecode.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
//display alert message in Web View
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
new AlertDialog.Builder(view.getContext())
.setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
}
public class JavaScriptInterface {
Context mContext;
// Instantiate the interface and set the context
JavaScriptInterface(Context c) {
mContext = c;
}
//using Javascript to call the finish activity
public void closeMyActivity() {
finish();
}
}
//Web view has record of all pages visited so you can go back and forth
//just override the back button to go back in history if there is page
//available for display
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I do not know in advance which files the users will be downloading other than that they will be pdf files.
Any help would be great
Thanks
I’m affraid the only solution is to use DownloadListener to catch the event and download the file manually.
Download a file example: http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
DownloadListener example: How do I use DownloadListener?

Android : Simple web application crashes on links other then the http scheme

I am a beginner in developing for the Android, so excuse me if this is obvious. I created a simple web application using the samples from android.com however, when a link like tel: or mailto: is clicked, the application crashes and askes me to force quit. I was wondering if anyone could tell me why.
public class MyApplication extends Activity {
WebView mWebView;
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost().equals("m.mydomain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the WebView class to display website */
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new InternalWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.mydomain.nl/");
}
}
Sorry to be so stupid, this actualy fixed it. With mailto, there is no host, thus getHost() will return null and causes the exception.
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}

Categories

Resources