实现将网页打包成一个apk
怕麻烦的话直接使用在线生成apk,简单快速省事,但会有别人的广告和水印,看的不舒服,还是自己写一个看的舒服。
网上的在线生成apk网址https://www.apicloud.com/autogeneration
Android studio代码
.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
webView = (WebView) findViewById(R.id.webView);
WebSettings ws = webView.getSettings();
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);//设置布局,会引起WebView的重新布局(relayout),默认值NARROW_COLUMNS
ws.setLoadsImagesAutomatically(true);//自动加载图片资源
ws.setJavaScriptCanOpenWindowsAutomatically(true);
ws.setJavaScriptEnabled(true);//执行javascript脚本
ws.setUseWideViewPort(true);//支持HTML的“viewport”标签或者使用wide viewport
ws.setLoadWithOverviewMode(true);//缩小内容以适应屏幕宽度
ws.setGeolocationEnabled(true);//启用定位
ws.setAppCacheEnabled(true);
ws.setDomStorageEnabled(true);//启用DOM存储API
webView.requestFocus();
webView.canGoForward();
webView.canGoBack();
webView.loadUrl("http://填你想嵌入的网址");
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return true;
}
});
}
}
.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_below="@+id/text_endLoading"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.test">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Test"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>