如果没有互联网连接,Android WebView离线消息
Android WebView offline message for if no internet connetion
我正在使用WebView开发android应用程序。若设备并没有互联网连接,我想显示自定义消息。我是新来的。我做了这段代码,但在用HTML格式编写时没有显示自定义消息。请帮我一步一步走。我正在使用安卓工作室2.1感谢提前
package ideainnovative.bdsoeg;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class web extends AppCompatActivity {
private WebView mywebview;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mywebview = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://google.com/");
mywebview.setWebViewClient(new WebViewClient());
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onBackPressed() {
if(mywebview.canGoBack()){
mywebview.goBack();
} else{
super.onBackPressed();
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"web Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://google.com")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
private void loadError() {
String html = "<html><body><table width='"100%'" height='"100%'" border='"0'" cellpadding='"0'" cellspacing='"0'">"
+ "<tr>"
+ "<td><div align='"center'"><font color='"red'" size='"20pt'">Your device don't have active internet connection</font></div></td>"
+ "</tr>" + "</table></html></body>";
System.out.println("html " + html);
String base64 = android.util.Base64.encodeToString(html.getBytes(),
android.util.Base64.DEFAULT);
mywebview.loadData(base64, "text/html; charset=utf-8", "base64");
System.out.println("loaded html");
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"web Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://ideainnovative.bdsoeg/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
使用以下方法检查互联网连接:
public boolean isNetworkAvailable(final Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
在将url加载到网络视图之前,请检查互联网连接。
if (isNetworkAvailable(context)) {
mywebview.loadUrl("http://google.com/");
} else {
loadError()
}
现在你的活动:
public class web extends AppCompatActivity {
private WebView mywebview;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mywebview = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
if (isNetworkAvailable(context)) {
mywebview.loadUrl("http://google.com/");
} else {
loadError()
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onBackPressed() {
if(mywebview.canGoBack()){
mywebview.goBack();
} else{
super.onBackPressed();
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"web Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://google.com")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
private void loadError() {
String html = "<html><body><table width='"100%'" height='"100%'" border='"0'" cellpadding='"0'" cellspacing='"0'">"
+ "<tr>"
+ "<td><div align='"center'"><font color='"red'" size='"20pt'">Your device don't have active internet connection</font></div></td>"
+ "</tr>" + "</table></html></body>";
System.out.println("html " + html);
myWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
System.out.println("loaded html");
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"web Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://ideainnovative.bdsoeg/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
public boolean isNetworkAvailable(final Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}
相关文章:
- 使用acess代币登录Facebook,并通过网络应用程序离线发布
- 回调函数在python代码中离线
- web浏览器中的离线应用程序存储数据并在之后上传
- OSM-离线查询我的IONIC/CORDOVA项目
- 访问离线jquery应用程序中的本地文件
- 独立离线的html5网络应用程序
- 如果没有互联网连接,Android WebView离线消息
- 如何离线存储数据库,在线时共享到服务器
- qoxdoo中的离线存储是否与所有浏览器和本地文件系统兼容
- Chrome离线不会使用cache.manifest
- 使用jquery将html文件加载到html中,用于离线/本地项目
- 任何可用作HTML5离线缓存链接的免费服务
- 在 Meteor.loginWithPassword 之后,旧用户不会离线(meteor-user-status 包)
- 如何使用一个代码库在线和离线访问 HTML5/JS 应用程序中的数据库
- JS离线检测?我做错了什么
- iOS版离线Jquery Mobile应用程序中处理导航的正确方法
- 在线/离线数据更新移动最佳实践
- Angularjs的离线网络应用程序
- 缓存以使我的网站离线是't工作
- 用Javascript或其他方法通过语音读取整个HTML页面(离线)