我想在 Internet 离线时显示对话框,但尝试执行此操作时出现错误……我的代码如下……你能告诉我我做错了什么吗?
这是我如何检查是否启用互联网的代码:
if (!isOnline())
{
showNoConnectionDialog(getApplicationContext());
//Toast.makeText(getApplicationContext(), "Internet connection is disabled!", Toast.LENGTH_LONG).show();
}
这段代码是我在我的应用程序中显示对话框的方式:
public static void showNoConnectionDialog(Context ctx1)
{
final Context ctx = ctx1;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setCancelable(true);
builder.setMessage(R.string.no_connection);
builder.setTitle(R.string.no_connection_title);
builder.setPositiveButton(R.string.settings_button_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton(R.string.cancel_button_text, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener()
{
public void onCancel(DialogInterface dialog) {
return;
}
});
builder.show();
}
public boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
{
return true;
}
return false;
}