2014-07-11 36 views
0

如何從cordova/phonegap webview中調用本地函數以用於顯示廣告。如何從cordova 3.x中調用native函數

編輯:好的我FUNALLY得到它,我會寫一些步驟,大家誰不知道該怎麼做(只是備用2天,您的一生:d)

A)如果你剛纔cordova/phonegap,並希望打電話給js,請執行以下操作:

1)將下列代碼替換爲現有的DroidGap活動。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.init(); // Calling this is necessary to make this work 
    appView.addJavascriptInterface(this, "MainActivity"); 

    /* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */ 

    super.loadUrl("file:///android_asset/www/index.html"); 
} 

2)在當前(this)活動中添加自定義函數,如下所示。

@JavascriptInterface 
public void customFunctionCalled() { 
    Log.e("Custom Function Called", "Custom Function Called"); 
} 

3)現在,從您的HTML/JavaScript代碼中調用該函數,如下所示。

window.MainActivity.customFunctionCalled(); 

B.1)如果你在網頁視圖中實現科爾多瓦/ PhoneGap的,並希望從JS調用做到這一點: (並想調用功能正常)

1)本添加到主Java文件:

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

2)聲明類JavaScriptInterface:

public class JavaScriptInterface { 
    private Activity activity; 

    public JavaScriptInterface(Activity activiy) { 
     this.activity = activiy; 
    } 

    @JavascriptInterface 
    public void showLog(){ 
     Log.v("blah", "blah blah"); 
    } 

} 

3)用`window.JSInterface.showLog();從js調用它。

B.2)如果你在網頁視圖中實現科爾多瓦/ PhoneGap的,並希望從JS調用(並要撥打的用戶界面功能,如舉杯)做到這一點:

1)本添加到主Java文件:

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

2)聲明類JavaScriptInterface:

public class JavaScriptInterface { 
    private Activity activity; 

    public JavaScriptInterface(Activity activiy) { 
     this.activity = activiy; 
    } 

    @JavascriptInterface 
    public void myFunction() 
    { 
     activity.runOnUiThread(new Runnable() { 
      public void run() { 
       //Code that interact with UI 
       showToast(); 
      } 
     }); 

    } 

} 

3)添加下面的吐司功能:

public void showToast(){ 
    Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", 
       Toast.LENGTH_LONG).show(); 
} 

4)用window.JSInterface.myFunction()調用它。

正如你看到的,如果你需要一個使用UI的函數,你需要將你的函數包裝到activity.runOnUiThread中,以便它可以從js中調用。

*如果你想從Java調用jQuery的方法做到這一點:

的Java:

cordova_webview.loadUrl("javascript:window.functionn()"); 

的Javascript:

window.function = punish; 

有一個愉快的一天!

回答

0

在。Java文件

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    super.init(); // Calling this is necessary to make this work 

    appView.addJavascriptInterface(this, "MainActivity"); 

    super.loadUrl(Config.getStartUrl()); 

} 

在JavaScript

window.MainActivity.customFunctionCalled(); 

這隻會在TargetSDK < 17工作。需要設置androidManifest.xml targetSDK < 17.對於TargetSDK> = 17,我想您需要創建一些自定義插件。我現在使用這個過程來降低我的目標SDK。

+0

我無法使用super.init(),因爲我將pgonegap集成到了一個webview中。如果我添加行,我會得到錯誤,我甚至無法構建應用程序。 –

+0

什麼是錯誤? –

相關問題