Get the WebView navigation event - android

I am working in an android application that can post tweets to twitter and I am doing it with the Web View widget. If the user is not logged in it will go to the login screen and if it a logged-in user in it will go to the tweet page. My requirement is after twetting from my application it should return to my application. How can I handle this situation by WebView. How will I get the redirect url from my WebView.
Please help me.
Please look into my code:
public class TestTwittershareActivity extends Activity {
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.twitter.com?status=");
webview.setWebViewClient(new HelloWebViewClient());
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

If I understand your problem correctly, you need a way to check whether the user is done with sending his tweets. Your best bet is to check the url that is being loaded in your webview. Hopefully, this url has some kind of indication that the tweet is done (maybe something in the status part?). To check the url you can use the HelloWebViewClient class you've already created and override it's onPageFinished method. e.g. something like this:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.contains("status=DONE")) {
// start your activity here
}
}
If you cannot detect based on the url that the user is finished, then things are much more complicated. In that case you can try to add a javascript that allows you to extract the html of the loaded page and you'll have to parse the html to look for clues if the user is done or not.

Related

How can I Block a Site in WebView

I have to make a webbrowser for android, so I want to try to block a site.
How can I do that?
Lets say your WebView id is myWebView then what you will do is this :
WebView wb = (WebView) findViewById(R.id.myWebView);
wb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("http://yourBlockedUrl.com")){
//notify the user that this url is blocked
return true;
}
return false;
}
});
by doing this you are overriding the url loading of your webview you can thus block a url from loading.
say you get the url sent as a string "www.google.com", from the edittext just do a check if this is a blocked url.
for example
if( "www.google.com".equalsIgnoreCase(blocked_string))
{
webview.setVisibility(View.GONE);
warning_view.setVisibilty(View.VISIBLE);
}
or you could try overriding the shouldOverrideUrlLoading() in the WebViewClient class
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(my_url.equals("www.google.com")){
//do something.
}
return true;
}
Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
public void gotoUrl(View view) {
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theUrl = theEditText.getText().toString();
//
// String blok= "http://www.teknojurnal.com";
webBrowserKu.loadUrl(theUrl);
}
my web browser when klik go, will process this, so what is the problem? why I cant do the steps from anything for blocking one site?

WebView back history without redirects

I implemented android webview and onKeyDown method for back key. (It implements webview.goBack();)
My problem is exactly similar to the question in this post below (no answers there)
How to control the Android WebView history/back stack?
PROBLEM - When I press back button, webview selects the previous URL, but if that URL was actually a redirect, it goes into this vicious cycle/loop. If you look at chrome or stock browser it correctly handles the back without going back to the redirects.
How can this be solved?
Example: go to gap.com. Then select "My Gap Credit Card". This opens a redirect link and then the final page. Now when I click back, it never goes to Gap.com home page.
Any suggestions...
Additional Information: I did implement the shouldOverrideUrlLoading. If I remove that method, it seems to work fine but with this method it does not...
I've just tested this on jellybean and it seems to work.
Essentially, whenever a new URL is loaded in the WebView keep a copy of the url.
On the next URL request, double check they we aren't already on this page, if they are, then go back in the webview history another step.
Essentially this is relying on the url passed into the override step being the redirected url, rather than the final redirected url.
public class MainActivity extends Activity {
private Button mRefreshButton;
private WebView mWebView;
private String mCurrentUrl;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mRefreshButton = (Button) findViewById(R.id.refresh);
mRefreshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mWebView.reload();
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(mCurrentUrl != null && url != null && url.equals(mCurrentUrl)) {
mWebView.goBack();
return true;
}
view.loadUrl(url);
mCurrentUrl = url;
return true;
}
});
mWebView.loadUrl("http://www.gap.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack()){
mWebView.goBack();
return true;
}
break;
}
}
return super.onKeyDown(keyCode, event);
}
}
I hope this answer if anyone is still looking for it.I had been hunting to fix similar issues in my project and had tried multiple approaches like using
- WebView.HitTestResult
- Pushing the urls into the list
- onKeyDown and so on...
I think most of it would work if your app consists of just webview. But my project had a combination of native and webview and handles some native schema.
Essentially found that the key is how you override the method shouldOverrideUrlLoading. Since i wanted my app to handles some of the urls and the webview to handle some of the other ones especially the back handling.I used a flag for back presses something like ..
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mClient.setIsBackPressed(true);
//mClient is an instance of the MyWebviewClient
mWebView.goBack();
} else {
super.onBackPressed();
}
}
public class MyWebviewClient extends WebViewClient {
private Boolean isBackPressed = false;
public void setIsBackPressed(Boolean isBackPressed) {
this.isBackPressed = isBackPressed;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isBackPressed){
return false;
}
else {
// handle the url by implementing your logic
return true;
}
}
#Override
public void onPageFinished(WebView view, String url) {
isBackPressed = false;
super.onPageFinished(view, url);
}
}
In this way, whenever there is a redirect when you click back, then it return false and hence mocks the behaviour of the webview. At the same time, you make sure that the isBackPressed is set to false after the page finishes loading.
Hope this helps !!

how to track an webpage when it is redirected from another web page within a web view in android

I load "www.gmail.com" in a webview,after login the a new webpage will be loaded i.e. our gmail account page.
I have to track that url when I submit login details and the new webpage is loading,I don't need any hard coded value to redirect to any webpage,I want to get that url when a webpage is loaded from another webpage,how can I achieve this.Please help me.
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
title_text=(TextView)findViewById(R.id.urltxt);
showWeb=(WebView)findViewById(R.id.webview_details_body);
showWeb.setWebViewClient(new HelloWeb());
showWeb.getSettings().setBuiltInZoomControls(true);
showWeb.getSettings().setLoadWithOverviewMode(true);//show the webpage in fullsize with all info
showWeb.getSettings().setUseWideViewPort(true);
WebSettings webSettings = showWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
showWebClick();
}
private void showWebClick() {
showWeb.loadUrl("http://www.gmail.com/");
}
public boolean onKeyDown(int keyCode,KeyEvent event){
if((keyCode==KeyEvent.ACTION_DOWN)&&showWeb.canGoBack()){
showWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class HelloWeb extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView vw,String url){
vw.loadUrl(url);
s=vw.getUrl();
title_text.setText( s);
return super.shouldOverrideUrlLoading(vw, url);
}
}
}
You can achieve this using method getUrl() of webview client.
But, This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed.
You can refer below link :
http://developer.android.com/reference/android/webkit/WebView.html#getUrl%28%29
So you have to call geturl method on onPageFinished method. It will be good.
#Override
public void onPageFinished(WebView view, String url) {
/*do your stuff here.*/
}
Your webview will most likely call either shouldOverrideUrlLoading or onLoadResource in its webviewclient with an url of its redirect.
If I understand your question correct, try overriding onLoadResource in your WebViewClient and look at the url parameter.
Using onLoadResource will also generate urls for other resources as well, such as images.

Disabling intent to call

I have a webview that loads a html page saved in the assets folder of my android application. I have some phone numbers that when tapped I wouldn't want a call activity call invoked. I thought editing the activity permissions in the manifest would help, but that's long winded.
well, try to override URL and in case it cause calling , so return true or any thing else .. to do you will need to create an extended class from WebViewClient and set it in webview:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView=(WebView)findViewById(R.id.webv);
webView.setWebViewClient(new ImWebViewClient());
//.....
}
class ImWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//check the url and when it will come with your phone number return true ..
if(url!=null && url.contain("123456789"))
return true;
return false;//means apply other mime type if available, and this may by default cause calling your number...
}
}

Android WebView - Intercept clicks

I have written a simple helloworld app with a WebView which has a link to CNN on a simple.html page in my asset folder.
cnn.com
How can I capture the click on this on my Activity, stop the WebView from navigating, and then inform the Activity that "http://CNN.com" was clicked?
Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:
WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);
// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
// you tell the webclient you want to catch when a url is about to load
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
// here you execute an action when the URL you want is about to load
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://cnn.com") ){
// do whatever you want
}
}
}

Categories

Resources