你好程序员在那里!
我的WebView-App出现了问题...
我想在启动时添加“连接检查”,当没有可用的Internet连接或WebView超时时,会弹出错误。我不能自己完成,因为我是Android编程的新手。
我还想添加一个选项菜单,我可以重新加载实际页面。
希望有人可以帮助我...
实际代码:
package net.schwarzis.htl_cloud;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("JavascriptInterface") public class MainActivity extends Activity {
/** Called when the activity is first created. */
private WebView browser = null;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
browser.loadUrl(url);
return true;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// connect to our browser so we can manipulate it
browser = (WebView) findViewById(R.id.mybrowser);
// get settings so we can config our WebView instance
WebSettings settings = browser.getSettings();
// JavaScript? Of course!
settings.setJavaScriptEnabled(true);
// clear cache
browser.clearCache(true);
final ProgressDialog pd=new ProgressDialog(this);
pd.setMessage("Lade...");
browser.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd.show();
browser.loadUrl(url);
return true;
}
});
// this is necessary for "alert()" to work
browser.setWebChromeClient(new WebChromeClient());
// add our custom functionality to the javascript environment
browser.addJavascriptInterface(new MyCoolJSHandler(), "Cloud");
// load a page to get things started
pd.show();
browser.loadUrl("http://cloud.schwarzis.net");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final ProgressDialog pd=new ProgressDialog(this);
pd.setMessage("Lade...");
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(browser.canGoBack() == true){
pd.show();
browser.goBack();
}else{
pd.show();
finish();
}
pd.dismiss();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
final class MyCoolJSHandler
{
// write to LogCat (Info)
public void Info(String str) {
Log.i("GoingNative",str);
}
// write to LogCat (Error)
public void Error(String str) {
Log.e("GoingNative",str);
}
// Kill the app
public void EndApp() {
finish();
}
}
}
答案 0 :(得分:2)
首先在清单中你应该有这些
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
关于你的onCreate
AlertDialog.Builder alertDialogBuilder = null;
将其放入onCreate
alertDialogBuilder = new AlertDialog.Builder(
this.context);
// set title
alertDialogBuilder.setTitle("No connection");
// set dialog message
alertDialogBuilder
.setMessage("No connection, Retry")
.setCancelable(true);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
然后使用此方法。
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
alertDialog.show();
return false;
} else
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl( "javascript:window.location.reload( true )" );
return true;
}
并刷新您的网页视图使用
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl( "javascript:window.location.reload( true )" );
以下是代码的精彩教程This。
答案 1 :(得分:1)
以下用户检查互联网连接的方法是否可用。
public boolean isNetworkAvailable() {
// Get Connectivity Manager class object from Systems Service
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// Get Network Info from connectivity Manager
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
您需要在AndroidMenifest.xml文件中提供以下权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
答案 2 :(得分:0)
要检查连接是否可用,您可以使用:
private boolean isNetAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
对于菜单中的刷新,请在<item>
中添加刷新res -> menu -> yourMenu.xml
,为其添加一个ID并在您的活动中添加onItemSelected()
(覆盖它)方法。