简体   繁体   English

将JavaScript值从Webview传递给Activity?Android

[英]Pass JavaScript Value from Webview to Activity?Android

I followed this to Print in android ( Thermal Printer ) 我按照在android( Thermal Printer )中打印

This is My web-view Over there On-click It will Load My Android Activity Which is Out of Web-view... 这是我的Web视图,在那上面单击将加载我的Android活动,该活动不在Web视图范围内...

For that I have Given this... to Open new activity from web-view.. 为此,我已经给出了...从网络视图中打开新活动。

public class Main_web extends AppCompatActivity {

private WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_m);

    webView = (WebView) findViewById(R.id.webm);

    WebSettings set = webView.getSettings();
    set.setJavaScriptEnabled(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    final ProgressDialog progressBar = ProgressDialog.show(Main_web.this, "Please Wait", "Loading...");

    webView.addJavascriptInterface(new WebAppInterface(this), "NewFUN");
    webView.requestFocusFromTouch();
    webView.setWebViewClient(new WebViewClient()

    {

        public void onPageFinished(WebView view, String url) {

            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

    });

    webView.setWebChromeClient(new WebChromeClient());

    String url = "www.google.com/m_app_eta.php";
    //String url = "file:///android_asset/tex.html";
    try {

        if (URLUtil.isValidUrl(url)) {
            webView.loadUrl(url);

        } else {
          //do something
        }
    } catch (Exception e) {
         //do something

    }

}

//Class to be injected in Web page

public class WebAppInterface {

    Context mContext;
    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void Print() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Are you sure you want to leave to next screen?");
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent chnIntent = new Intent(Main_web.this, Print_activity.class);
                        startActivity(chnIntent);
                    }
                });
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        alertDialog.show();
    }

}
}

Now My printer Activity is working Fine So here I want to pass a Value On click Button Which Opens Printer... But can Any one suggest me How to Receive same value in android Activity not in web-view... 现在我的打印机活动正常,所以在这里我想传递一个值来打开打印机的单击按钮...但是任何人都可以建议我如何在android活动中接收相同的值而不是在网络视图中...

All I need is In my web-view when I click on the Button its loading Android Activity Along with that I want to Pass A string value to new Activity 我需要的是在我的Web视图中,单击Button时,它正在加载Android Activity,同时我还希望将字符串值传递给新的Activity。

Why Should I pass Value means In that I have a Url so I can Print with that URL Here JS value is different form current URL 为什么我应该传递Value的意思是,因为我有一个网址,所以我可以使用该URL打印此处,JS值与当前URL不同

Update 更新资料

I followed this But its Inside the Web-view I want same at Outside the web-view... 我遵循了这一点,但我想要在网络视图外的内容相同...

Means When I click on the web-view Its opening android activity with the same its should pass a value to my activity not the web-view 意思是当我单击Web视图时,其打开的android活动与之相同,应该将一个值传递给我的活动,而不是Web视图

Update 1 更新1

I followed @Sujal Mandal answer But I dont Know How to use that Value In next activity Can any one suggest me... on this kind... in next activity It may in System.out.println or text-view or any other string So I can use can Any one suggest How to Use the JavaScript Value in other activity outside the web-view.. 我跟随@Sujal Mandal回答但我不知道如何在下一个活动中使用该值任何人都可以在下一个活动中向我建议此类...在下一个活动中它可能在System.out.printlntext-view或任何其他text-view string因此我可以使用任何人都可以建议如何在Web视图之外的其他活动中使用JavaScript值。

HTML JS CODE HTML JS代码

<script type="text/javascript">
function Pa(value) {
       //value is the param received from onClick
       NewFUN.Print(value); //call the android method with value param
    }
</script>

<center>
    <h3>Sample HTML</h3>
    <div id="content">Click on Button To thermal print</div>
    <div>
        <input type="button" onClick="Pa('26997')" /><br/>
        </div>
</center>   

& change your android code to be like this 并更改您的android代码,就像这样

@JavascriptInterface
    public void Print(final String stringFromWebView) {
        //use stringFromWebView
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Are you sure you want to leave to next screen?");
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent chnIntent = new Intent(Main_web.this, Print_activity.class);
                        chnIntent.putExtra("STRING_DATA", stringFromWebView);
                        startActivity(chnIntent);
                    }
                });
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

In next activity receive the web JS response by using 在下一个活动中,通过使用接收Web JS响应

String data = getIntent().getStringExtra("STRING_DATA");

at oncreate 在oncreate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM