显示Internet设置的对话窗口

时间:2013-02-27 19:33:18

标签: android dialog

我希望在互联网离线时显示对话框,但是在尝试执行此操作时出错...我的代码正在关注... 你能告诉我我做错了吗?

这里是代码我如何检查是否启用了互联网:

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;
}

1 个答案:

答案 0 :(得分:1)

它对我有用。

替换此

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

享受:)