使用vue开发webapp,通过android的webview嵌入到android APP中
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layerType="software"/>
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//页面开始加载时
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
//页面加载结束时
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// 这里进行无网络或错误处理,具体可以根据errorCode的值进行判断,
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
/**
* 网页跳转:
* 1.在当前的webview跳转到新连接
* view.loadUrl(url);
* 2.调用系统浏览器跳转到新网页
* Intent i = new Intent(Intent.ACTION_VIEW);
* i.setData(Uri.parse(url));
* startActivity(i);
*/
return true;
}
});
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);// 支持JS
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置缓存
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过js打开新的窗口
webSettings.setBuiltInZoomControls(false);// 设置支持缩放
webSettings.setDomStorageEnabled(true);//使用localStorage则必须打开
webSettings.setSupportMultipleWindows(false);// 设置同一个界面
webSettings.setNeedInitialFocus(false);// 禁止webview上面控件获取焦点(黄色边框)
webSettings.setDatabaseEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setSupportZoom(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
// mWebView.setBackgroundColor(ContextCompat.getColor(this,android.R.color.transparent));
// mWebView.setBackgroundResource(R.color.colorAccent);
// REMOTE RESOURCE
// mWebView.loadUrl("http://www.baidu.com");
// mWebView.setWebViewClient(new MyWebViewClient());
//忽略跨域
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}else{
try {
Class<?> clazz = mWebView.getSettings().getClass();
Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
if (method != null) {
method.invoke(mWebView.getSettings(), true);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// LOCAL RESOURCE
mWebView.loadUrl("file:///android_asset/index.html");
// mWebView.setWebViewClient(new MyWebViewClient());
// mWebView.clearHistory();
// mWebView.clearCache(true);
// mWebView.clearFormData();
// mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);// 隐藏滚动条webView.requestFocus();
// mWebView.requestFocusFromTouch();
//浏览器中调试
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
mWebView.setWebContentsDebuggingEnabled(true);
}
}
}
设置不显示android默认头部
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
在androidManifest.xml文件中进行以下设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zyl.webapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
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"
android:hardwareAccelerated="false">
<!--解决canvas无法绘制https://blog.csdn.net/ge673551532/article/details/76103686-->
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在build.gradle中可以设置版本区间
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.zyl.webapp"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '28.0.3'
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}