1.前言
XWalkView是Crosswalk提供的webview,可用于替代系统原生的webview
公司做的网页是用Angular2js,导致在原生android 的webview运行不起来
腾讯的x5内核也运行不起来,最后偶然发现CrossWalk的XWalkView
2.使用步骤
在app的build.gradle中,添加maven地址和complie
- ndk {
abiFilters "armeabi-v7a"
} - repositories {
maven { url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2' }
} - compile 'org.xwalk:xwalk_core_library:23.53.589.4'
4.添加权限
<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.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
5.MainActivity
package com.tuojie.webview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Toast;
import org.xwalk.core.XWalkView;
public class MainActivity extends AppCompatActivity {
private XWalkView mXWalkView;
private long lastClickBackTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.webview);
mXWalkView.getNavigationHistory().clear(); //清除历史记录
mXWalkView.setDrawingCacheEnabled(false); //不使用缓存
mXWalkView.clearCache(true); //清楚包括磁盘缓存
mXWalkView.load("-----", null); //写入要加载的网址
}
@Override
protected void onPause() {
super.onPause();
if (mXWalkView != null) {
mXWalkView.pauseTimers();
mXWalkView.onHide();
}
}
@Override
protected void onResume() {
super.onResume();
if (mXWalkView != null) {
mXWalkView.resumeTimers();
mXWalkView.onShow();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mXWalkView != null) {
mXWalkView.onDestroy();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mXWalkView != null) {
mXWalkView.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onNewIntent(Intent intent) {
if (mXWalkView != null) {
mXWalkView.onNewIntent(intent);
}
}
//监听返回键
@Override
public void onBackPressed() {
if (System.currentTimeMillis() - lastClickBackTime > 2000) { // 后退阻断
Toast.makeText(getApplicationContext(),"再点一次退出应用",Toast.LENGTH_LONG).show();
lastClickBackTime = System.currentTimeMillis();
} else { // 关掉app
super.onBackPressed();
}
}
}
Sum:有问题欢迎您指教,谢谢你