五步整合crosswalk内核浏览h5变高性能APP.
crosswalk介绍就不提了。我这里提供5个步骤整合到android项目中去。
开发环境: Android Studio
1.build.gradle Module配制
repositories {
maven {
url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.xwalk:xwalk_core_library:23.53.589.4'
}
2.manifest.xml文件
需要注意:android:hardwareAccelerated="true" 为硬件加速
<!--需要权限-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:hardwareAccelerated="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
3.布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dowell.myapplication.MainActivity">
<org.xwalk.core.XWalkView
android:id="@+id/xwalkWebView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
4.java代码
setContentView(R.layout.activity_main);
xwalkWebView=(XWalkView)findViewById(R.id.xwalkWebView);
xwalkWebView.load("http://www.baidu.com",null);
XWalkSettings settings = xwalkWebView.getSettings();
settings.setDomStorageEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAllowFileAccess(true);
settings.setDatabaseEnabled(true);
settings.setJavaScriptEnabled(true);
//资源加载动作的事件 参考webview的setWebViewClient
//xwalkWebView.setResourceClient(new XWalkResourceClient());
//界面相关的client 参照WebView setWebChromeClient
//xwalkWebView.setUIClient(new MyXWalkUIClient(mXwalkView));
//js 交互相关,参照webview的addJavascriptInterface代码
//xwalkWebView.addJavascriptInterface(new UMengInterface(), "UMeng");
5.屏蔽crosswalk自动响应android返回键的事件。内置带回退效果
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
return true;
}
return super.dispatchKeyEvent(event);
}
参考:https://github.com/crosswalk-project/crosswalk-test-suite/blob/7b8527f7551548e7225700e78a11b82c371ae137/usecase/usecase-embedding-android-tests/embeddingapi/src/org/xwalk/embedded/api/sample/XWalkViewWithDispatchKeyEvent.java