Android原生软件里嵌入一个基于cordova的网页页面(webview)
2016-12-12 14:20
1066 查看
本篇文章翻译自cordova官网:其地址为:http://cordova.apache.org/docs/en/3.6.0/guide/platforms/android/webview.htmlAndroid webview这篇文章指导用户在Android原生软件里嵌入一个基于cordova的网页页面(webview)。这些部件是怎样在互相之间交流数据请查看:Application Plugins如果你不熟悉安卓开发,那你就需要先学习Android开发环境搭建,使用一下网址学习:http://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html至少安装Android SDK,在你开发网页嵌入应用之前。自从Cordova 1.9开始。安卓平台依赖组件“CordovaWebView”。1. 跟随这些说明,确保你安装了最新的cordova版本。,从cordova.apache.org下载并解压。2. 切换到解压出来文件夹的/framework目录,然后运行ant jar。就会创建出一个叫Cordova.jar的文件。文件被放置在目录/framework/cordova-x.x.x.jar。3. 复制这个.jar文件到Android项目的/libs目录4. 然后编辑Android项目目录的/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
以下代码展示了一个依靠接口的基本应用。展示了怎样通过ID调用之前创建的界面文件里的控件。代码如下:
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.
如果当前网页页面需要调用摄像头,请实现一下部分。@Overridepublic void setActivityResultCallback(CordovaPlugin plugin) {this.activityResultCallback = plugin;}/*** Launch an activity for which you would like a res4000ult 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 resultsif (command != null) {this.keepRunning = false;}// Start activitysuper.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目录下。
相关文章推荐
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
- Android构建一个通用的WebView(二):自定义的错误页面、缓存数据,离线浏览
- android基于webview嵌入html5的字体大小设置
- Android webView H5页面分享 微博找不到网页 自定义协议
- iOS 基于JavaScriptCore 不等webView加载完毕就交互,网页获取原生内容。 webView的高级用法之JS交互,js与oc的相互调用
- Android使用原生组件WebView加载网页和数据的方法
- android 通过webView,简单的通过网页唤起本地界面,如果比较多的话最好不要用原生的webView和js去搭建,用框架去做
- android webView 每次开启一个新的页面(重定向问题)
- android------引导页两种实现方式(原生和WebView网页实现)
- android------引导页两种实现方式(原生和WebView网页实现)
- android webView加载页面时显示出全部网页内容
- android webView加载页面时显示出全部网页内容
- Android中WebView在加载网页的时候,怎样应用本地的CSS效果?就是说怎样把本地的CSS嵌入到HTML中
- Android中WebView返回到上一个网页
- Android Webview 加载一个页面时,报错 java.lang.Throwable: EventHub.removeMessages(int what = 105) is not suppo
- Android 浏览网页:WebView 嵌入浏览器(浏览历史返回、自定义加载失败界面、支持缩放、获取标题栏)
- webView 无限进入,点击返回,返回到网页的上一个页面,当无返回时,返回到自己的页面
- Android中替换WebView加载网页失败时的页面
- Android 编程下 WebView 加载一个网页如何得到网页的 Cookie 值
- Android webview里加载显示网页,显示进度,点击网页链接跳转到app页面