我在片段中使用webview,我想添加互联网连接错误信息和加载pregress栏,有人可以请帮助 这是我的
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class FoodFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_food, container, false);
WebView webView = (WebView) view.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://food.pacho.co.ke/");
return view;
}
}
答案 0 :(得分:1)
你可以这样做:
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
然后在您要检查连接的代码中:
if(isNetworkAvailable())
{
// do things when connection is available
}
else
{
final android.app.AlertDialog.Builder alertDialog = new
android.app.AlertDialog.Builder(project_details.this);
alertDialog.setMessage("You don't have internet connection").setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
alertDialog.show();
}
此外,我认为您必须在清单中添加权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
当你有网络但实际上没有互联网连接时,我不确定这是否有效。