显示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();
    }

享受:)

最新更新