Cordova支持Android WebViews

原文:http://cordova.apache.org/docs/en/7.x/guide/platforms/android/webview.html

Cordova19以後,安卓平臺依靠CordovaWebView控件

Cordova19以前,依賴於CordovaActivity組件

 

1. 保證安裝最新Cordova版本,從cordova.apache.org下載並解壓其安卓包

2. 找到安卓的/framework目錄,運行ant jar,創建Cordova的.jar文件,則未/framework/cordova-x.x.x.jar

3. 拷貝.jar文件到安卓項目的/libs目錄下

4. 添加布局到應用/res/xml/main.xml文件

<org.apache.cordova.CordovaWebView
    android:id="@+id/tutorialView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

5. 修改activity,以讓其實現CordovaInterface接口。也可以拷貝/framework/src/org/apache/cordova/CordovaActivity.java,或者自己實現它們。以下代碼段是基於其接口最簡單的應用實現:

public class CordovaViewTestActivity extends Activity implements CordovaInterface {
    CordovaWebView cwv;
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cwv = (CordovaWebView) findViewById(R.id.tutorialView);
        Config.init(this);
        cwv.loadUrl(Config.getStartUrl());
    }

6. 如果該應用需要用到攝像頭,可以以以下方式實現:

@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
    this.activityResultCallback = plugin;
}
/**
 * Launch an activity for which you would like a result when it finished. When this activity exits,
 * your onActivityResult() method is called.
 *
 * @param command           The command object
 * @param intent            The intent to start
 * @param requestCode       The request code that is passed to callback to identify the activity
 */
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
/**
 * Called when an activity you launched exits, giving you the requestCode you started it with,
 * the resultCode it returned, and any additional data from it.
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param data              An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    CordovaPlugin callback = this.activityResultCallback;
    if (callback != null) {
        callback.onActivityResult(requestCode, resultCode, intent);
    }
}

7. 最後,記得加上線程池,否則插件沒有線程跑咯:

@Override
public ExecutorService getThreadPool() {
    return threadPool;
}

8. 將HTML和js文件拷貝到對應的安卓/assets/www目錄中

9. 將framework/res/xml中的config.xml文件拷貝到/res/xml目錄中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章