[英]Unable to call my Java class from Javascript In my Android App
我不知道下面的代碼有什么問題試圖使用webview addJavainterface從javascript代碼中調用我的java類,但似乎無法正常工作,我嘗試按照android網站上的教程進行操作,但仍然沒有運氣幫助,我實際上並不知道缺少的是..這是我的代碼...我的JavaScript代碼是這樣的:
public class HomeFragment extends Fragment {
private WebView webView;
private Bundle webViewBundlehome;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_layout, container, false);
webView =(WebView) rootView.findViewById(R.id.webViewHome);
WebSettings websettings = webView.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setDomStorageEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(),"Android");
if(webViewBundlehome==null){
webView.loadUrl("file:///android_asset/www/index.html");
}
else
{
webView.restoreState(webViewBundlehome);
}
return rootView;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
webViewBundlehome = new Bundle();
webView.saveState(webViewBundlehome);
}
public class JavaScriptInterface {
private Activity activity;
public void opencart(String cart_id){
Intent intent = new Intent(activity, Cartegory.class);
intent.putExtra("cart_id", cart_id);
startActivity(intent);
}
public void openproduct(String pro_id){
Intent intent = new Intent(activity,view.class);
intent.putExtra("pro_id", pro_id);
startActivity(intent);
}
}}
$('#mybutton').on("click",function(){
Android.openproduct(product_id);
})
android:targetSdkVersion="19"
既然你要定位API 19,你需要來裝飾opencart
和openproduct
與Java方法@JavascriptInterface
。 有關詳細信息,請參見此處 。
但是看起來像
class JsObject {
@JavascriptInterface
public String toString() { return "injectedObject"; }
}
我還會做一些簡單的事情(例如一條日志消息),以確保javascript調用正常工作; 然后更新為開始活動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.