2013-01-11 18 views
4

我是Android新手。如何實現CordovaInterface,以便我可以在webview中使用相機?

我按照tutorial在我的Android應用中嵌入了Cordova WebView

我已經使用CordovaWebView從我的服務器成功加載了一個網頁。

假設我在該網頁上有一個名爲「捕獲照片」的按鈕,應該怎麼做才能調用本地API以便我可以使用相機?

本教程建議我需要實現CordovaInterface以如下方式使用相機。

@Override 
public void setActivityResultCallback(CordovaPlugin plugin) { 
    this.activityResultCallback = plugin;   
} 

我不知道究竟是什麼activityResultCallback。是否有另一個教程向我展示如何實現這個接口?

回答

7

由於沒有人回答我的問題。

我找到一個tutorial可以解決這個問題。

更新: 鑑於鏈接已損壞,我將發佈我自己的代碼以實現Cordova界面。

// Instance for CordovaInterface 
private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
private boolean mAlternateTitle = false; 
private boolean bound; 
private boolean volumeupBound; 
private boolean volumedownBound; 
private CordovaPlugin activityResultCallback; 
private Object activityResultKeepRunning; 
private Object keepRunning; 


public Activity getActivity() { 
    return this; 
} 

@Deprecated 
public Context getContext() { 
    return this; 
} 

public ExecutorService getThreadPool() { 
    return threadPool; 
} 


public void setActivityResultCallback(CordovaPlugin plugin) { 
    this.activityResultCallback = plugin; 

} 

public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) { 
    this.activityResultCallback = command; 
    this.activityResultKeepRunning = this.keepRunning; 

    // If multitasking turned on, then disable it for activities that return 
    // results 
    if (command != null) { 
     this.keepRunning = false; 
    } 

    // Start activity 
    super.startActivityForResult(intent, requestCode); 

} 

@Override 
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    final CordovaPlugin callback = this.activityResultCallback; 
    if (callback != null) { 
     // Need to use background thread 
     this.getThreadPool().execute(new Runnable() { 
      public void run() { 
       callback.onActivityResult(requestCode, resultCode, intent); 
      } 
     }); 
    } 
} 
+0

我一直在嘗試使用phonegap 3.0的地理位置插件。我收到一個錯誤「插件應該使用CordovaInterface.getThreadPool()」。 直到幾天後我才發現這個錯誤。突然開始得到它。我是否應該爲我希望使用的每個插件實現CordovaInterface,並且我該怎麼做? –

+0

獲得nathvarun在這裏提到的同樣的錯誤。 – Aron

+0

鏈接已損壞。 –

相關問題