所以我有一个基于webview的应用程序。我的问题是,当网络丢失时调用OnReceivedError
时,我想
当用户在对话框中单击“确定”以检查连接时,
web.reload();
finish();
由以下决议解决:
将警报对话框更改按钮更改为以下
.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
问题在于以下
DialogInterface paramDialogInterface,int paramInt)
上下文上下文;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
...
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
Webview_Main.this);
localBuilder2.setTitle(R.string.webview_error_received_title);
localBuilder2.setMessage(R.string.webview_error_received);
localBuilder2.setIcon(R.drawable.ic_launcher);
localBuilder2.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
if(isNetworkAvailible(context))
web.reload();
else
finish();
}
};
localBuilder2.show();
};
isNetworkAvailible Class
public boolean isNetworkAvailible(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return false;
}
return true;
}
答案 0 :(得分:2)
由于代码中的 此 关键字引用了DialogInterface.OnClickListener,因此没有为该类型定义getSystemService,因此必须从引用当前内容的上下文变量调用getSystemService活动,例如,像这样:
context.getSystemService(Activity.CONNECTIVITY_SERVICE)
答案 1 :(得分:1)
纠正我的回答:
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
Webview_Main.this);
localBuilder2.setTitle(R.string.webview_error_received_title);
localBuilder2.setMessage(R.string.webview_error_received);
localBuilder2.setIcon(R.drawable.ic_launcher);
localBuilder2.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface paramDialogInterface,int paramInt) {
if(haveInternet(context))
web.reload();
else
finish();
}
});
localBuilder2.show();
};
public boolean haveInternet(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return false;
}
return true;
}
答案 2 :(得分:0)
我可能会错过很多时间,但似乎你正在创建一个DialogInterface.OnClickListener
,但没有指定它来处理任何事情。
更改您的代码:
localBuilder2.setPositiveButton(R.string.alert_dialog_ok, null);
localBuilder2.setOnClickListener(new DialogInterface.OnClickListener() {
...
});
答案 3 :(得分:0)
检查网络可用性
在你的Activtiy中
if(CheckNetwork.isInternetAvailable(ActvityName.this))// pass activity context
{
//do soemthing
}
如果需要检查非活动类中的网络可用性,则需要将活动上下文作为参数传递给类构造函数并使用活动上下文。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
MyAnotherClass = new MyAnotherClass(MainActivtity.this);
}
在你的MyAnotherClass
中 Class MyAnotherClass
{
Context context;
public MyAnotherClass(Context activitycontext)
{
this.context= activitycontext;
if(CheckNetwork.isInternetAvailable(context))
{
//do something
}
}
}
CheckNetwork类
public class CheckNetwork {
private static final String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}
}
}
}