Android Display Web View - java

Hi I'm working on an android application that uses a web view. It works perfectly on most API levels, but I'm having problems on API 9 (Gingerbread 2.3.5). Where it's really inconsistent to whether it works or not. I've used the following code for it to work on froyo:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(urlString)) {
// 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;
}
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
but this doesn't get hit when debugging on API level 9 (Gingerbread 2.3.5). Has anyone else had this issue? if so what was causing it and how did you fix it?
Thanks

Related

Launching a open a browser when clicking a link in android webview

I'm trying to make a link inside my android webview load into one of the web browsers available onto the device. What is happening is that it just loads the link inside the webview. How can I make the link load outside the webview of my application?
The link that I am need to make work is this:
Forgot your password?
When I click it nothing happens, I need it to call the default web browser of the device.
My shouldOverrideUrlLoading is currently being used to check the URL and loads another activity if it is equal:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("extraData", "the urls"+Uri.parse(url).getPath());
if (Uri.parse(url).getPath().equals("/qr_code")) {
progress.setVisibility(View.GONE);
Intent intent = new Intent(getApplicationContext(),QRActivity.class);
startActivityForResult(intent, 1);
setContentView(R.layout.activity_qr);
//return true;
}
if(Uri.parse(url).getPath().equals("/linka")){
return true;
}
return false;
}
Update: It is now working but for some reason my current webView still runs the link therefore the pages loads to the link clicked
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("extraData", "the urls"+Uri.parse(url).getPath());
if (Uri.parse(url).getPath().equals("/TMMobile/Main/qr_code")) {
//return true;
}
if(Uri.parse(url).getPath().equals("/linka")){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://testdomain/RecoverPassword.aspx"));
startActivity(browserIntent);
}
return false;
}
}
To open a URL in a browser, just can use the following code:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

webview never call ShuldOverrideUrlloading in webviewclient in android

I have a webview and a link on the URL that I have loaded on that webview, I want such like that if user clicks in this link the app will go to the background and open that link with the default web browser of that device. To do this I have done the following:
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.getSettings().setPluginState(PluginState.ON);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null
&& url.startsWith("http://the6figuremarketer.com/apps/androidfb21/thks.html")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
web.loadUrl("http://the6figuremarketer.com/apps/androidfb21/index.html");
but I figured out that the shouldoverrideurl method is nevet get called. what should I do now? can anyone help?
Look at shouldOverrideUrlLoading in WebView for Android not running.
shouldOverrideUrlLoading() is not called when you call loadurl on webview. It is called only when you click on a link in webview.

How to save a cookie in an Android webview forever?

With my code below, I have been able to save a cookie, but as soon as I close the application the cookie disappears.
How is this caused and how can I solve it?
package com.jkjljkj
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(getBaseContext());
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
CookieSyncManager.getInstance().startSync();
CookieSyncManager.getInstance().sync();
//This will load the webpage that we want to see
webview.loadUrl("http://");
}
}
You have to tell the CookieSyncManager to sync after it has loaded the page in question. In your sample code, the onCreate method executes completely before the WebView tries to load the page, so the sync process (which happens asynchronously) will probably complete before the page is loaded.
Instead, tell the CookieSyncManager to sync onPageFinished in the WebViewClient. That should get you what you want.
The CookieSyncManager Documentation is a good read for how to do this properly.
Here is how you could set up your WebViewClient implementation to do this for you:
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
);
You would not need to tell the CookieSyncManager to sync elsewhere with this in place. I haven't tested this, so let me know if it works.
.sync() is to force a imediate sync ,and must be called after page load cause it sync RAM with cache , so cookie must be in ram before calling it .
System automatically sync it every 5 mintues if you use this scheme
onCreate:
CookieSyncManager.createInstance(context)
onResume:
CookieSyncManager.getInstance().startSync()
onPause:
CookieSyncManager.getInstance().stopSync()
I think you did not waited 5 mintues so system save cookie.
For those who are facing Problems with CookieManager class to make cookie persist even after closing the app, they should try the flush() function of CookieManager.
Please note that i haven't tried this, so if it works then please let me know too.
According to android documentation
void flush()
Ensures all cookies currently accessible through the getCookie API are written to persistent storage. This call will block the caller until it is done and may perform I/O.
Also in CookieSyncManager documnentation it is written that:
This class was deprecated in API level 21.
The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method flush() which is a synchronous replacement for sync(). CookieSyncManger link
Works perfectly for API 20+
myWebView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl){
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView webView, String url){
CookieManager.getInstance().flush();
}
}
);

Issue with Facebook sdk login in android

ImageView connect = (ImageView) findViewById(R.id.fconnect);
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
facebook.authorize(SignIn.this, new String[] {"offline_access", "email", "read_friendlists","publish_stream" },new DialogListener() {
#Override
public void onComplete(Bundle values) {
String AccessToken = facebook.getAccessToken();
LoginDirect = "Loading Home....";
LoginProcessChkUserStatus();
}
#Override
public void onFacebookError(FacebookError error) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onCancel() {
}
});
}else{
progress = true;
LoginProcessChkUserStatus();
}
}
});
this the facebook api....that i use for loading in my application...this works fine...when i click login button...after authorizing it comes to oncomplete stage... now the problem comes when i installed separtely Facebook.apk in my phone taken from Facebook SDK....the view becomes this....also when i click login button it never excutes the above code....what shall i do...???
I have also faced some similar issue when I have integrated facebook with my application. when I am clicking the facebook icon in my application native facebook app was launched and when I uninstalled that native facebook app all were working correctly. I figured a way out to overcome this issue by the following method and I have posted it on below link on stackoverflow: "An error's occurred" when authenticating with Facebook's android sdk . Actually my problem was, when I used the debug key, the Key Hash value I entered was wrong in the facebook app register. When I corrected the key hash according to what I have posted in the above link my issue was solved. Please try this also.
The changing of screens is normal. If you have the Facebook app installed, the SDK uses this to log in. If not, it uses a WebView for authentication (as seen on your first screenshot).
And why does this not work? The Facebook app uses result codes from Androids activity mechanism.
I dont see onActivityResult() in your code. Make sure to have that implemented in your activity. It should look like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
After that, your code should work as intended. :)

Categories