2016-04-29 36 views
1

我已經使用this指導將JavaScript代碼綁定到Android代碼,該代碼正常工作(一個Toast消息由javascript代碼觸發並顯示double類型的javascript代碼輸出)。我的問題是我不能在代碼的其他部分使用輸出。例如,正確的輸出是10, 20, 30, ...,但我的代碼另一部分進入是0, 10, 20, 30, ...。這裏是有關我的部分代碼:無法在Android代碼中正確訪問javascript輸出

public class myFragment extends Fragment { 
    int globalVar; 
    . 
    . 
    . 
    //some android code 
    . 
    . 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_item_show: 
      loadNew(); 
      Toast.makeText(getActivity(), "globalVar " + String.valueOf(globalVar), Toast.LENGTH_SHORT).show(); 
    //after clicking menu item, I receive a double output from javascript, 
    //but first output is zero!!! 
     . 
    . 
    . 
    //some android code 
    . 
    . 
public class WebAppInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c) { 
     mContext = c; 
    } 
    @JavascriptInterface 
    public void showToast(double toast) { 
     Toast.makeText(mContext, String.valueOf(toast), Toast.LENGTH_SHORT).show(); 
     //In this part of code, results are correct 
     globalVar = (int) (toast); 
    } 
} 

每次我點擊菜單項,一個新的頁面加載和一個javascript代碼給出了一個新的結果。

<body onLoad="showAndroidToast();"> 

我認爲這個問題起源於訪問內部類的變量,或者是另一個原因。 預先感謝您。

+0

嗨,你的'showAndroidToast()'方法是什麼樣的?既然你在onLoad事件中有它,它會在你每次加載頁面時執行。因此,也許你應該寧可有一個按鈕,並在您的文檔,比如'onClick事件處理程序’)」 />
' – ishmaelMakitla

+0

感謝您回答@ishmaelMakitla。在showAndroidToast()裏面,我定義了一個雙精靈,然後調用Android.showToast(myVar)。使用按鈕不會做任何更改,因爲在'公共無效showToast(雙烤麪包)'我收到正確的結果。 –

+0

我看到了,你說你不能在你的另一部分代碼中使用輸出(double值) - 這個「代碼的另一部分」在哪裏似乎有不正確的數字'0,10,20,30,...' ?我懷疑這是在菜單項目點擊功能,但只是爲了確認,所以我可以建議一些東西...... – ishmaelMakitla

回答

0

// Java的結束//

上創建網頁視圖Acivity的

webView.getSettings().setJavaScriptEnabled(true); 
webView.addJavascriptInterface(new JavaScriptInterface(this), "JSInterface"); 

//Class to be injected in Web page 
    public class JavaScriptInterface implements JavascriptCallback { 
     Context mContext; 

     /** 
     * Instantiate the interface and set the context 
     */ 
     JavaScriptInterface(Context c) { 
      mContext = c; 
     } 

     /** 
     * Show a toast from the web page 
     */ 
     @JavascriptInterface 
     public void loadComplete() { 
      pd.dismiss(); 
     } 
// this code will use to show toast from javascript 
     @JavascriptInterface 
     public void showToast(String toast) { 
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
     } 
     // this code will use to call a java code from javascript with some parameters. 
     @JavascriptInterface 
     public void getTimeandDistance(String time, String distance) { 

      disTime = "Time: " + time + " | Distance: " + distance; 
      runOnUiThread(new Thread(new Runnable() { 
       public void run() { 
        try { 
         someTextview.setText(disTime); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      })); 

     } 
    } 

// Java腳本結束//

function someFunction(data) { 

     var distance = data.route.distance; 

     var time = data.route.formattedTime; 

     // In this way we call a java code from javascript with some parameters. 

     JSInterface.getTimeandDistance(time + "", distance + ""); 

} 

,只是顯示敬酒,你可以做這樣

// write anywhere in your javaScript code 
JSInterface.showToast("Show Toast"); 
+0

感謝您的回答@Hamza Mehmood。 JavascriptCallback無法解析。 –

0

我的建議是你用一些「listener」或「callback」(通過作爲第二個參數WebAppInterface)將在收到通知showAndroidToast方法。我創建了一個Gist of such a listener只需將其添加到您的軟件包並更新軟件包名稱。 然後更新您的內部類(WebAppInterface)取第二個參數的構造函數中:

public class WebAppInterface { 
    Context mContext; 
    DummyToastListener toastListener; 
    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c, DummyToastListener listener) { 
     mContext = c; 
     toastListener = listener; 
    } 
    @JavascriptInterface 
    public void showToast(double toast) { 
     Toast.makeText(mContext, String.valueOf(toast), Toast.LENGTH_SHORT).show(); 
     toastListener.onToastValue(toast); 
    } 
} 

則進行更新myFragment類來實現DummyToastListener這樣的:

public class myFragment extends Fragment implements DummyToastListener { 

    //some where in your code, initializing the WebAppInterface 
    //the second argument is also a 'this' because the fragment implements the interface 
    webView.addJavascriptInterface(new WebAppInterface(this, this), "Android"); 
    //implementing the method of the interface DummyToastListener 
    @Override 
    public void onToastValue(double value){ 
    //got the new value from Javascript function, do whatever... 
    Toast.makeText(getActivity(), String.valueOf(value), Toast.LENGTH_LONG).show(); 
    } 

} 

通過這種方法,值從Javascript函數總是會在onToastValue回調方法中新鮮出來。