-1
我正在开发一个使用WebView的android应用程序。如果设备没有互联网连接,我想显示自定义消息。我是新的。我编写了HTML格式的代码,但没有显示自定义消息。请帮助我再见一步。我采用了android 2.1的工作室感谢推进Android WebView离线消息,如果没有互联网连接
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();
}
}
对不起大哥,我不清楚如何设置。我需要删除加载代码?我尝试但不工作。请问您可以为此页面添加一个完整或请编辑我的代码。感谢您的回复 –