我关注了this在 android 中打印(热敏打印机)
这是My web-view在那里,单击它将加载我的 Android Activity,该 Activity 不在 Web View 中...
为此,我给出了这个...从 WebView 中打开新 Activity ..
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();
}
}
}
现在我的打印机 Activity 工作正常所以在这里我想传递一个打开打印机的单击按钮上的值...但是任何人都可以建议我如何在android Activity 中而不是在 WebView 中接收相同的值...
我需要的只是在我的 WebView 中,当我单击按钮时,它会加载 Android Activity,同时我想将字符串值传递给新 Activity
为什么我应该传递值意味着我有一个网址,这样我就可以使用该网址进行打印 这里的 JS 值与当前 URL 不同
更新
我关注了this但它在 WebView 内部,我希望在 WebView 外部也有相同的...
意味着当我点击 WebView 时,它打开的android Activity 与其相同,它应该将一个值传递给我的 Activity 而不是 WebView
更新1
我遵循@Sujal Mandal的回答,但我不知道如何在下一个 Activity 中使用该值,任何人都可以建议我...在这种情况下...在下一个 Activity 中它可能在System.out.println
或 text-view
或任何其他 string
所以我可以使用 can 任何人建议如何在 WebView 之外的其他 Activity 中使用 JavaScript 值..
最佳答案
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>
&将你的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();
}
在下一个 Activity 中使用接收 Web JS 响应
字符串数据 = getIntent().getStringExtra("STRING_DATA");
在创建时
关于javascript - 将 JavaScript 值从 Webview 传递到 Activity?Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42663484/