我想在 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;
}
最佳答案
它对我有用。
替换这个
if (!isOnline())
{
showNoConnectionDialog(getApplicationContext()); // Error is here...
//Toast.makeText(getApplicationContext(), "Internet connection is disabled!", Toast.LENGTH_LONG).show();
}
与
if (!isOnline())
{
showNoConnectionDialog(this);
//Toast.makeText(getApplicationContext(), "Internet connection is disabled!", Toast.LENGTH_LONG).show();
}
享受 :)
关于android - 显示 Internet 设置的对话框窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120762/