4

我是安卓新手。

我按照教程WebView在我的 Android 应用程序中嵌入了 Cordova 。

我已经使用CordovaWebView.

假设我在那个网页上有一个名为“Capture Photo”的按钮,我应该怎么做才能调用本地 API 才能使用相机?

本教程建议我需要按CordovaInterface以下方式实现使用相机。

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

我不知道activityResultCallback到底是什么。是否有另一个教程向我展示如何实现此接口?

4

1 回答 1

7

因为没有人回答我的问题。

我找到了可以解决这个问题的教程。

更新:鉴于链接已损坏,我将发布我自己的代码来实现 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);
            }
        });
    }
}
于 2013-01-14T17:29:30.997 回答