activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|center_horizontal|center_vertical"
android:background="@drawable/bj119201080"
tools:ignore="ExtraText,MissingConstraints">
<LinearLayout
android:id="@+id/main"
android:layout_width="160sp"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="120sp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="32sp"
android:layout_gravity="center|top"
android:background="@drawable/shebei"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="32sp"
android:layout_gravity="center|top"
android:layout_marginTop="6sp"
android:background="@drawable/ip"
android:orientation="vertical">
<EditText
android:id="@+id/tab1editTextIP"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="56sp"
android:background="@android:color/transparent"
android:ems="19"
android:text="192.168.1.199"
android:textColor="@color/white"
android:textSize="14sp"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="22sp"
android:orientation="vertical">
<Button
android:id="@+id/tab1buttonConnect"
android:layout_width="70sp"
android:layout_height="30sp"
android:layout_weight="1"
android:background="@drawable/selector_green_bg"
android:focusableInTouchMode="false"
android:textColor="@color/gray"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<com.wang.avi.AVLoadingIndicatorView
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:indicatorName="LineSpinFadeLoaderIndicator"/>
</RelativeLayout >
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.visualizedh5app;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.wang.avi.AVLoadingIndicatorView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private boolean isSuccess = false;
private boolean isError = false;
private Button tab1buttonConnect;
private LinearLayout main;
public EditText ip;
private SharedPreferences mSps;
private AVLoadingIndicatorView avi;
private long time =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
avi = (AVLoadingIndicatorView) findViewById(R.id.load);
avi.hide();
tab1buttonConnect = (Button) findViewById(R.id.tab1buttonConnect);
main = (LinearLayout) findViewById(R.id.main);
ip = (EditText) findViewById(R.id.tab1editTextIP);
mSps = MainActivity.this.getSharedPreferences("share", MODE_PRIVATE);
String strIp = mSps.getString("ip", "192.168.1.12");
ip.setText(strIp);
ip.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
SharedPreferences.Editor editor = mSps.edit();
editor.putString("ip", ip.getText().toString());
editor.commit();
}
});
//连接按钮点击事件
tab1buttonConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setVisibility(View.GONE);//隐藏
//main.setVisibility(View.VISIBLE);显示
avi.show();
String url = "http://" + ip.getText().toString();
System.out.println("--------------" + url);
setUpView(url);
//加载h5页面结束
}
});
}
@SuppressLint("SetJavaScriptEnabled")
public void setUpView(String url) {
//加载需要显示的网页
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webView.loadUrl(url);
webSettings.setJavaScriptEnabled(true); //设置WebView属性,运行执行js脚本
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);//设定支持viewport
webSettings.setLoadWithOverviewMode(true); //自适应屏幕
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);//设定支持缩放
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不读取缓存
webView.setBackgroundColor(ContextCompat.getColor(this,android.R.color.transparent));
webView.setBackgroundResource(R.color.black);
webView.setWebViewClient(webClient); //设置Web视图
}
// webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);不读取缓存
WebViewClient webClient = new WebViewClient() {
//设置打开网页时,不调用系统浏览器进行打开,而是在本WebView中直接显示。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
// Toast.makeText(MainActivity.this, "开始加载", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!isError) {
isSuccess = true;
System.out.println("网页打开成功");
setContentView(webView);
//回调成功后的相关操作
} else {
isSuccess = false;
System.out.println("网页打开失败");
startActivity(new Intent(MainActivity.this, MainActivity.class));// 留在当前页面
Toast.makeText(MainActivity.this, "提示:连接失败,请检查地址及网络设置后重试。", Toast.LENGTH_LONG).show();
}
avi.hide();
}
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
isError = true;
System.out.println("网页回调失败");
//回调失败的相关操作
}
};
/**
* 双击返回桌面
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - time > 1000)) {
if(isSuccess){
Toast.makeText(this, "再按一次断开连接并返回连接页面", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
}
time = System.currentTimeMillis();
} else {
if(isSuccess){
webView.destroy();
startActivity(new Intent(MainActivity.this, MainActivity.class));
} else {
// finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
}
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.visualizedh5app">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.Notitle_FullScreen"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
https://blog.csdn.net/weixin_40438421/article/details/85700109
|