1.下载并安装android studio
2.点击"New Project",新建一个安卓项目
选择"Empty Activity",然后点击"Next"
修改一下app名称和包名,改成自己的,开发语言选择Java
3.新建一个java类,命名为"WebViewClientDiy"
代码如下:
package com.example.test;
import android.content.Context;
import android.webkit.WebViewClient;
public class WebViewClientDiy extends WebViewClient {
Context context;
public WebViewClientDiy(Context context){
this.context =context;
}
}
import android.content.Context;
import android.webkit.WebViewClient;
public class WebViewClientDiy extends WebViewClient {
Context context;
public WebViewClientDiy(Context context){
this.context =context;
}
}
4.把MainActivity的代码改成下面这段代码
package com.example.test;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClientDiy(this));
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//设置去缓存
//设置响应js 的Alert()函数
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.d("MainActivity","newProgress:"+ newProgress );
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Log.d("MainActivity","标题:"+ title);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setCancelable(false);
b.create().show();
return true;
}
//设置响应js 的Confirm()函数
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
b.create().show();
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
final JsPromptResult result) {
// mReusultText.setText("Prompt input is :" + message);
result.confirm();
return super.onJsPrompt(view, url, message, message, result);
}
});
webView.loadUrl("https://www.zhiboblog.com");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if(url.startsWith("http:") || url.startsWith("https:") ) {
view.loadUrl(url);
return false;
}else{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
});
setContentView(webView);
}
/**
* 使点击回退按钮不会直接退出整个应用程序而是返回上一个页面
*
* */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
webView.goBack();//返回上个页面
return true;
}
return super.onKeyDown(keyCode, event);//退出整个应用程序
}
}
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClientDiy(this));
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//设置去缓存
//设置响应js 的Alert()函数
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.d("MainActivity","newProgress:"+ newProgress );
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Log.d("MainActivity","标题:"+ title);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setCancelable(false);
b.create().show();
return true;
}
//设置响应js 的Confirm()函数
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
b.create().show();
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
final JsPromptResult result) {
// mReusultText.setText("Prompt input is :" + message);
result.confirm();
return super.onJsPrompt(view, url, message, message, result);
}
});
webView.loadUrl("https://www.zhiboblog.com");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if(url.startsWith("http:") || url.startsWith("https:") ) {
view.loadUrl(url);
return false;
}else{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
});
setContentView(webView);
}
/**
* 使点击回退按钮不会直接退出整个应用程序而是返回上一个页面
*
* */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
webView.goBack();//返回上个页面
return true;
}
return super.onKeyDown(keyCode, event);//退出整个应用程序
}
}
6.在AndroidManifest.xml中添加两行代码
这句添加到application上面
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
这句添加到application节点中
android:usesCleartextTraffic="true"
7.去掉标题栏
找到res/values下面的themes.xml,把DarkActionBar改成NoActionBar
8.修改app图标
把做好的app图标命名为ic_launcher.png,然后放到drawable目录下,最后再改一下AndroidManifest.xml中的icon,改成"@drawable/ic_launcher"
9.生成apk
点"Build"下面的"Build APK"
生成好后就会在右下角弹出一个提示,这时候点一下"locate"就可以打开生成好的apk所在的目录
以上就是h5网站封装打包成安卓app的教程