0

I am working on WebView. I don't know whether it's possible or not. I am opening an url inside of this view which is basically a login page. Now want to get the information whether user has successfully logged in or not. If yes then navigate to custom page (Android native) else keep on that page.

Thanks in advance.

rupesh
  • 2,789
  • 4
  • 21
  • 48
  • You can use JS to parse that information and then send it to the native Android side using Java-JS bridge http://developer.android.com/intl/ko/guide/webapps/webview.html#BindingJavaScript – Eugene Popovich Aug 17 '15 at 07:46
  • @user527759 thanks for quick response. but i will come to know whether he has successfully logged in or not. I have no control over webview. – rupesh Aug 17 '15 at 07:49
  • You can parse HTML DOM or use some Javascript function in your page to provide that information – Eugene Popovich Aug 17 '15 at 07:51
  • @user527759 are you talking about login page to put some javascript function. Then i have no control if you are saying for my code. Where and how you want me put that piece of code. – rupesh Aug 17 '15 at 07:53
  • You can load any Javascript code to the loaded page using `loadUrl("javascript:")` call or `evaluateJavascript` call in Kit-Kat and above http://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – Eugene Popovich Aug 17 '15 at 07:56

3 Answers3

1

We can achieve it via using this..

  1. Create a function of javascript in the HTML page of Login Success/Failue HtmL page.
  2. in this javascript function ,call your native Function by Using addJavascriptInterface:
 package com.example.androidjsdemo;

 import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

 public class MainActivity extends ActionBarActivity {


public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        // Here you can Start WhatEver Activty launch///////
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webMy = (WebView) findViewById(R.id.webMy);
    webMy.addJavascriptInterface(new WebAppInterface(this), "Android");
    WebSettings setting = webMy.getSettings();
    setting.setJavaScriptEnabled(true);
    //File file = new File("andoid");
    webMy.loadUrl("file:///android_asset/mine.html");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

 mine.html


<HTML>
<script>
function called()
{
  // alert("hi");
  Android.showToast("Hi Testing");
}
</script>
<Body>
Hi Sumit
<button onclick="called()">click me</button>
</Body>
</HTML>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
 android:layout_height="match_parent"

 tools:context="com.example.androidjsdemo.MainActivity" >

 <WebView
    android:id="@+id/webMy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

 </RelativeLayout>
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
0

When user complete successful singing he should redirect to other web page with url that indicate the login was successful. it can look something like http://www.yourwebsite.com/islogin?yes. than all you need to do is to check in your app what the login status by verify that the url contain islogin and that it set to yes and do whatever you want.

here a small sample to help you begin:

webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(isUserLoggedIn(url)){
                doWork();
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        }

        public void onPageFinished(WebView view, String url) {
        }
    });
nbaroz
  • 1,685
  • 11
  • 14
0

For Example below is your login URL which you load in webview for login

String loginURL = "http://www.myurl.com/login";

Two possibility, when user click on login button

1) Success

Navigate to Other page as per web code. So we need to check it and open our native application screen.

2) Fail

Same login page but display error message according to user input.

webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains(loginURL)) {
                    // Login Fail
                    return false;
                } else {
                    // Login Success 
                    // Open Android Native screen
                    return true;
                }
            }
        });
Android Team
  • 11,274
  • 2
  • 26
  • 46