您的位置:首页 > 其它

crosswalk工程建立及使用

2015-08-18 16:10 423 查看
crosswalk是一个开源项目,github可以找到源代码,是一个webView控件。这玩意弥补了webkit的不足,其实就是对于4.0到4.4版本的5.0的chromiun浏览器一个打包

crosswalk简介详情请见:http://dev.yesky.com/24/39285024.shtml

话不多说,上代码:



首先创建一个xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<widget xmlns     = "http://www.w3.org/ns/widgets"
id        = "io.cordova.helloCordova"
version   = "2.0.0">
<name>Hello Cordova</name>

<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>

<author href="http://cordova.io" email="dev@cordova.apache.org">
Apache Cordova Team
</author>

<access origin="*"/>

<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="index.html" />

<preference name="loglevel" value="DEBUG" />
<!--
<preference name="splashscreen" value="resourceName" />
<preference name="backgroundColor" value="0xFFF" />
<preference name="loadUrlTimeoutValue" value="20000" />
<preference name="InAppBrowserStorageEnabled" value="true" />
<preference name="disallowOverscroll" value="true" />
-->

<feature name="App">
<param name="android-package" value="org.apache.cordova.App"/>
</feature>
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker"/>
</feature>
<feature name="Device">
<param name="android-package" value="org.apache.cordova.Device"/>
</feature>
<feature name="Accelerometer">
<param name="android-package" value="org.apache.cordova.AccelListener"/>
</feature>
<feature name="Compass">
<param name="android-package" value="org.apache.cordova.CompassListener"/>
</feature>
<feature name="Media">
<param name="android-package" value="org.apache.cordova.AudioHandler"/>
</feature>
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.CameraLauncher"/>
</feature>
<feature name="Contacts">
<param name="android-package" value="org.apache.cordova.ContactManager"/>
</feature>
<feature name="File">
<param name="android-package" value="org.apache.cordova.FileUtils"/>
</feature>
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.NetworkManager"/>
</feature>
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.Notification"/>
</feature>
<feature name="Storage">
<param name="android-package" value="org.apache.cordova.Storage"/>
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.FileTransfer"/>
</feature>
<feature name="Capture">
<param name="android-package" value="org.apache.cordova.Capture"/>
</feature>
<feature name="Battery">
<param name="android-package" value="org.apache.cordova.BatteryListener"/>
</feature>
<feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.SplashScreen"/>
</feature>
<feature name="Echo">
<param name="android-package" value="org.apache.cordova.Echo"/>
</feature>
<feature name="Globalization">
<param name="android-package" value="org.apache.cordova.Globalization"/>
</feature>
<feature name="InAppBrowser">
<param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>

<feature name="Calculator">
<param name="android-package" value="com.example.calculator.Calculator"/>
</feature>

<!-- Deprecated plugins element. Remove in 3.0 -->
<plugins>
</plugins>
</widget>


完整copy进去。

next,

activity的layout图纸:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<!-- <org.apache.cordova.CordovaWebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></org.apache.cordova.CordovaWebView> -->

<com.example.webviews.IWeb
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</RelativeLayout>


非常简单,,然后整合代码:

为了做扩展,我们需要一个自定义的webView而不是源生的:

代码如下:

package com.example.webviews;

import org.apache.cordova.CordovaWebView;

import android.content.Context;
import android.util.AttributeSet;

public class IWeb extends CordovaWebView{

public IWeb(Context context) {
super(context);
}

public IWeb(Context context, AttributeSet attrs) {
super(context, attrs);
}

public IWeb(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public IWeb(Context context, AttributeSet attrs, int defStyle,
boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
}

}


activity代码:

package com.example.webviews;

import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaChromeClient;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.WebView;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends CordovaActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

appView = (CordovaWebView) findViewById(R.id.webview);

CordovaWebViewClient webClient = makeWebViewClient(appView);
CordovaChromeClient chromeClient = makeChromeClient(appView);
appView.getSettings().setJavaScriptEnabled(true);
appView.setWebViewClient(webClient);
appView.setWebChromeClient(chromeClient);
webClient.setWebView(appView);
chromeClient.setWebView(appView);

//		appView.loadUrl("http://www.hao123.com/?tn=95334564_hao_pg");
appView.loadUrlIntoView("http://wap.vip.com/");
//		loadUrl("http://www.hao123.com/?tn=95334564_hao_pg");
}

@Override
protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {

CordovaWebViewClient client = new CordovaWebViewClient(this,appView){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
};

return client;
}

@Override
protected CordovaChromeClient makeChromeClient(CordovaWebView webView) {

CordovaChromeClient client = new CordovaChromeClient(this, appView){

@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
result.confirm();
return true;
}

};

return client;
}

}


OK了,,以上就大功告成了,,发现还缺一些东西,比如开源的代码库:

因为数量较多,我提供一个项目结构树跟下载!

下载地址:http://download.csdn.net/detail/u010819959/9019973
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: