如何在Android应用程序中检测网页表单提交 [英] How Do I Detect a Webpage Form Submission In Android App

查看:129
本文介绍了如何在Android应用程序中检测网页表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Android编写了一个应用程序,它使用webview通过复选框将信息提交给MYSQl后端。现在要退出Web视图,用户必须使用设备上的按键继续下一个应用程序活动。
有没有一些方法来检测表单提交按钮已被应用程序按下,然后使用一些东西,说一个意图去下一个活动(关闭webview,然后NextActivity.class),最好没有javascript?
我尝试了一个shouldOverrideUrlLoading,当按下submit按钮时调用一个新页面,然后用(Uri.parse(url).getHost()。equals(page url)){意图...}没有运气。
提前致谢!
这里是编码页面:

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++;
        }
    }


推荐答案

Got它工作,这是其他人的修补程序:

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++;
        }
    }

}

这篇关于如何在Android应用程序中检测网页表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆