作者:xiejiahui62272 | 来源:互联网 | 2023-06-07 16:15
我创建了一个简单的Webview应用程序,该应用程序在没有网络时显示资产中的error.html,但是一旦网络可用,用户将无法继续使用webview。用户只是停留在error.html页面中。我现在想尝试做的是使Webview应用程序检查网络并在没有网络连接时显示“ no internet connection”,并在网络可用时重新加载。
Manifest.xml
private WebView mWebView;
SwipeRefreshLayout swipe;
private void setfullScreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setflags(
WindowManager.LayoutParams.flaG_FULLSCREEN,WindowManager.LayoutParams.flaG_FULLSCREEN
);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
LoadWeb(mWebView.getUrl());
}
});
LoadWeb("http://m.mary.org");
}
public void LoadWeb(String url){
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavascriptEnabled(true);
mWebView.loadUrl(url);
swipe.setRefreshing(true);
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url,String userAgent,String contentDisposition,String mimetype,long contentLength) {
DownloadManager.Request myRequest = new DownloadManager.Request(Uri.parse(url));
myRequest.allowScanningByMediaScanner();
myRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager myManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
myManager.enqueue(myRequest);
Toast.makeText(Mainactivity.this,"Your File is downloading....",Toast.LENGTH_SHORT).show();
}});
mWebView.setWebViewClient(new com.example.mary.MyAppWebViewClient(){
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view,String url) {
//hide loading image
swipe.setRefreshing(false);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
@Override
public void onBackpressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackpressed();
}
}
}
Mainactivity.java
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url) {
if(Uri.parse(url).getHost().endsWith("m.mary.org")) {
return false;
}
Intent intent = new Intent(Intent.actION_VIEW,Uri.parse(url));
view.getcontext().startactivity(intent);
return true;
}
MyAppWebViewClient.java
{{1}}
创建广播接收器:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.HashSet;
import java.util.Set;
public class NetworkStateReceiver extends BroadcastReceiver {
protected Set listeners;
protected Boolean connected;
public NetworkStateReceiver() {
listeners = new HashSet();
cOnnected= null;
}
public void onReceive(Context context,Intent intent) {
if(intent == null || intent.getExtras() == null)
return;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
cOnnected= true;
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
cOnnected= false;
}
notifyStateToAll();
}
private void notifyStateToAll() {
for(NetworkStateReceiverListener listener : listeners)
notifyState(listener);
}
private void notifyState(NetworkStateReceiverListener listener) {
if(cOnnected== null || listener == null)
return;
if(cOnnected== true)
listener.networkAvailable();
else
listener.networkUnavailable();
}
public void addListener(NetworkStateReceiverListener l) {
listeners.add(l);
notifyState(l);
}
public void removeListener(NetworkStateReceiverListener l) {
listeners.remove(l);
}
public interface NetworkStateReceiverListener {
public void networkAvailable();
public void networkUnavailable();
}
}
在您的活动中像这样使用它:
private NetworkStateReceiver.NetworkStateReceiverListener networkStateReceiverListener = new NetworkStateReceiver.NetworkStateReceiverListener() {
@Override
public void networkAvailable() {
//todo: refresh webview
}
@Override
public void networkUnavailable() {
//todo: show error
}
};
清单中的权限: