Cordova支持Android WebViews

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

Cordova1.9以后,安卓平台依靠CordovaWebView控件

Cordova1.9以前,依赖于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目录中



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值