1、新建一空白项目,empty
2、项目填写,www.eesai.com,名称AsaiWeb,layout名asaiweb.xml,JAVA名AsaiWeb.java
3、项目建立后在app-mainfests中找到AndroidManifest.xml,粘贴以下代码:
代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eesai.www.asaiweb">
<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">
<activity android:name=".AsaiWeb">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
</manifest>
4、找到app-res-layout中找到asaiweb.xml,粘贴以下代码:
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/asaiweb_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="bottom|right"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:id="@+id/back"
android:background="#000000"
android:textColor="#FFFFFF"
android:text="BACK"
/>
</LinearLayout>
</RelativeLayout>
5、找到app-java-com.eesai.www.asaiweb中找到AsaiWeb,粘贴以下代码:
代码:
package com.eesai.www.asaiweb;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class AsaiWeb extends AppCompatActivity {
private WebView webView;
private long exitTime = 0;
public void onBackPressed() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
//彻底关闭整个APP
int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
System.exit(0);
} else {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
}
}
}
public static boolean isConnect(Context context) {
boolean _isConnect = false;
ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if (network != null) {
_isConnect = conManager.getActiveNetworkInfo().isAvailable();
}
return _isConnect;
}
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
this.getSupportActionBar().hide();//解决标题栏有时候没法去掉的问题
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉顶部时间等信息栏
super.onCreate(savedInstanceState);
setContentView(R.layout.asaiweb);
this.webView = (WebView) findViewById(R.id.asaiweb_webview);
View vback = findViewById(R.id.back);
vback.getBackground().setAlpha(58);
vback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
WebSettings webSettings = webView.getSettings();
webView.addJavascriptInterface(this,"wv");
webSettings.setJavaScriptEnabled(true);// 让WebView能够执行javaScript
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);// 让JavaScript可以自动打开windows
webSettings.setDomStorageEnabled(true);//开启 DOM 存储功能
webSettings.setAppCacheMaxSize(1024*1024*88);//设置缓冲大小,88M
webSettings.setDatabaseEnabled(true);//开启 数据库 存储功能
webSettings.setAppCacheEnabled(true);//开启 应用缓存 功能
webSettings.setDatabasePath("/asai/cache/asaiweb");// 设置数据库缓存路径
webSettings.setAppCachePath("/asai/cache/asaiweb");//设置 应用 缓存目录
webSettings.setDefaultTextEncodingName("UTF-8");// 离线文件访问编码
webSettings.setAppCacheEnabled(true);// 设置缓存
if (!isConnect(AsaiWeb.this)) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}else{
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
}
// 设置缓存模式,有缓存先读缓存
// LOAD_DEFAULT: 如果我们应用程序没有设置任何cachemode, 这个是默认的cache方式。 加载一张网页会检查是否有cache,如果有并且没有过期则使用本地cache,否则 从网络上获取。
// LOAD_CACHE_ELSE_NETWORK: 使用cache资源,即使过期了也使用,如果没有cache才从网络上获取。
// LOAD_NO_CACHE: 不使用cache 全部从网络上获取
// LOAD_CACHE_ONLY: 只使用cache上的内容。
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);//设置渲染效果优先级,高
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// 支持内容重新布局,一共有四种方式
// 默认的是NARROW_COLUMNS
// 1.NARROW_COLUMNS:可能的话使所有列的宽度不超过屏幕宽度
// 2.NORMAL:正常显示不做任何渲染
// 3.SINGLE_COLUMN:把所有内容放大webview等宽的一列中
webSettings.setSupportZoom(true);// 支持缩放(适配到当前屏幕)
webSettings.setUseWideViewPort(true);// 将图片调整到合适的大小
webSettings.setDisplayZoomControls(true);// 设置可以被显示的屏幕控制
webSettings.setLoadWithOverviewMode(true);
// webSettings.setDefaultFontSize(12);// 设置默认字体大小
// 设置WebView的客户端
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
webView.loadUrl(url);
return true;
}
//网络请求部分
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
url = url.toLowerCase();
if (url.contains("77ya.com") || url.contains("eesai.com")) {
return super.shouldInterceptRequest(view, url);
} else {
//return new WebResourceResponse(null,null,null); //去掉广告
return null; //去掉广告
}
}
}
);
if (!isConnect(AsaiWeb.this)) {
webView.loadUrl("file:///android_asset/_ks.html");//这里加载本地
} else {
webView.loadUrl("http://x.77ya.com/");
}
}
}
6、菜单栏中找到Build-Build APK生成安卓APK安装包,下到自己的手机上安装即可。