How Do I Detect a Webpage Form Submission In Andro

2019-09-13 07:18发布

I've written an app for Android that uses a webview to submit information via check boxes to a MYSQl back end. Right now to exit from the webview, the user has to use the keys on the device to continue to the next app activity. Is there some way to detect the form submission button has been pressed by the app and then use something, say an Intent to go to the next activity (closes webview and then NextActivity.class), preferably without javascript? I've tried a shouldOverrideUrlLoading by having a new page called when the submit button is pressed, then calling an if statement with (Uri.parse(url).getHost().equals("page url")){Intent...} with no luck. Thanks in advance! Here is the coded page:

public class WebViewActivity extends Activity { 
private WebView webView;
int backButtonCount = 0;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    //webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
    Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String rode=extras.getString("rode");
             }
        String ray = extras.getString("rode");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://mywebsite.com/SubDir/user_man.php?user=" + ray);

}


private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (Uri.parse(url).equals("http://mywebsite.com/SubDir/process_prof.php")) {
             return false;
        }

        Intent ik = new Intent(WebViewActivity.this, master_menu.class);
        startActivity(ik);
        finish();
            return true;
    }
}   

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar_two, menu);
        return true;
    }

    @Override
    public boolean    onOptionsItemSelected(MenuItem item) {
    if  (item.getItemId()==R.id.action_selected){ 
        Intent iw = new Intent(this,GetUserEmail.class);
    startActivity(iw);
    finish();
    }
    else

        if  (item.getItemId()==R.id.action_cancel){ 
            Intent iw = new Intent(this,RegisterActivity.class);
        startActivity(iw);
        finish();
        }
        return true;
    }   
    public void onBackPressed()
    {
        if(backButtonCount >= 1)
        {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(this, "Press the back button again to close.", Toast.LENGTH_SHORT).show();
            backButtonCount++;
        }
    }

2条回答
We Are One
2楼-- · 2019-09-13 08:00

Got it working, here is the fix for others:

public class WebViewActivity extends Activity {

public WebView webView;
int backButtonCount = 0;
final Handler myHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String rode=extras.getString("rode");
                     }
        String ray = extras.getString("rode");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);

    webView.loadUrl("http://mywebsite.com/subDir/user_man.php?user=" + ray);
    webView.setWebViewClient(new WebViewClient());
    webView.addJavascriptInterface(myJavaScriptInterface, "ForwardFunction");
}


  public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;  }

        public void goHome(){
                Intent ik = new Intent(WebViewActivity.this, GetUserEmail.class);
                startActivity(ik);
                finish();
                Toast.makeText(WebViewActivity.this,"Your choices have been updated", Toast.LENGTH_LONG).show();
        }
  }


 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar_two, menu);
        return true; }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    if(item.getItemId()==R.id.action_selected)
    {   Intent iw = new Intent(this,GetUserEmail.class);
        startActivity(iw);
        finish(); }
    else
        if(item.getItemId()==R.id.action_cancel)
        { 
        Intent iw = new Intent(this,RegisterActivity.class);
        startActivity(iw);
        finish();
        }
    return true;
    }   
    public void onBackPressed()
    {
        if(backButtonCount >= 1)
        {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(this, "Press the back button again to close.", Toast.LENGTH_SHORT).show();
            backButtonCount++;
        }
    }

}

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-13 08:04

I believe the issue with your if statement is the getHost() which will return only the host name (www.google.com) and not the whole URL. Try removing that and I believe your if statement will return true if it matches.

查看更多
登录 后发表回答