Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要求用户连接到internet或退出应用程序(android)_Android_Android Internet - Fatal编程技术网

要求用户连接到internet或退出应用程序(android)

要求用户连接到internet或退出应用程序(android),android,android-internet,Android,Android Internet,我正在开发一个图像库应用程序,其中的应用程序正在从internet检索图像 所以我想提示一个对话框,要求用户连接到internet或退出应用程序 向用户显示WiFi和运营商网络选项。首先,您应该检查它们是否已连接(大量示例如何在线执行此操作) 如果不是,则使用此代码 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Connect to wifi or quit") .setCance

我正在开发一个图像库应用程序,其中的应用程序正在从internet检索图像

所以我想提示一个对话框,要求用户连接到internet或退出应用程序


向用户显示WiFi和运营商网络选项。

首先,您应该检查它们是否已连接(大量示例如何在线执行此操作)

如果不是,则使用此代码

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Connect to wifi or quit")
.setCancelable(false)
.setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
   }
 })
.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        this.finish();
   }
});
 AlertDialog alert = builder.create();
 alert.show();

这将检查wifi和移动数据。在splash或您的主要活动上运行tis代码以检查网络连接。如果网络未连接,则弹出对话框并完成活动。就这么简单

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
试试这个:

public static boolean isConnected(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if ((wifiInfo != null && wifiInfo.isConnected()) || (mobileInfo != null && mobileInfo.isConnected())) {
        return true;
}else{
        showDialog();
         return false;
}

private void showDialog()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Connect to wifi or quit")
        .setCancelable(false)
        .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
           }
         })
        .setNegativeButton("Quit", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                this.finish();
           }
        });
         AlertDialog alert = builder.create();
         alert.show();
}

就是这样做的

此类检查是否存在有效的internet连接:

public class ConnectionStatus {

    private Context _context;

    public ConnectionStatus(Context context) {
        this._context = context;
    }

    public boolean isConnectionAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }
}
public void addListenerOnWifiButton() {
        Button btnWifi = (Button)findViewById(R.id.btnWifi);

        iia = new ConnectionStatus(getApplicationContext());

        isConnected = iia.isConnectionAvailable();
        if (!isConnected) {
            btnWifi.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    Toast.makeText(getBaseContext(), "Please connect to a hotspot",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        else {
            btnWifi.setVisibility(4);
            warning.setText("This app may use your mobile data to update events and get their details.");
        }
    }
public void addListenerOn3GButton() {
    Button btnThreeGee = (Button)findViewById(R.id.btn3G);
    iia = new ConnectionStatus(getApplicationContext());

    isConnected = iia.isConnectionAvailable();
    if (!isConnected) {
        btnThreeGee.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
            Toast.makeText(getBaseContext(), "Please check 'Data enabled' option",
                    Toast.LENGTH_SHORT).show();
        }
    });
    }
    else {

         btnThreeGee.setVisibility(4);
         cont.setVisibility(View.VISIBLE);
         warning.setText("This app may use your mobile data to update events and get their details.");
        }
}
如果没有有效的internet连接,以下方法将打开3G面板:

public class ConnectionStatus {

    private Context _context;

    public ConnectionStatus(Context context) {
        this._context = context;
    }

    public boolean isConnectionAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }
}
public void addListenerOnWifiButton() {
        Button btnWifi = (Button)findViewById(R.id.btnWifi);

        iia = new ConnectionStatus(getApplicationContext());

        isConnected = iia.isConnectionAvailable();
        if (!isConnected) {
            btnWifi.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    Toast.makeText(getBaseContext(), "Please connect to a hotspot",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        else {
            btnWifi.setVisibility(4);
            warning.setText("This app may use your mobile data to update events and get their details.");
        }
    }
public void addListenerOn3GButton() {
    Button btnThreeGee = (Button)findViewById(R.id.btn3G);
    iia = new ConnectionStatus(getApplicationContext());

    isConnected = iia.isConnectionAvailable();
    if (!isConnected) {
        btnThreeGee.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
            Toast.makeText(getBaseContext(), "Please check 'Data enabled' option",
                    Toast.LENGTH_SHORT).show();
        }
    });
    }
    else {

         btnThreeGee.setVisibility(4);
         cont.setVisibility(View.VISIBLE);
         warning.setText("This app may use your mobile data to update events and get their details.");
        }
}

希望这能有所帮助:)

尽管很简单,只要检查是否有活动的网络连接即可。如果不是,则显示“是|否”对话框。如果为正,则显示网络设置。否则,完成活动。非常抱歉,但我的代码也会检查场景中是否需要任何internet连接。嘿,伙计,你知道如何以类似于蓝牙/GPS提示符的对话框类型启用/禁用数据漫游设置提示符吗?您的回答很好,但它会打开一个新的活动,而不仅仅是一个破坏用户体验的小对话框。我认为答案将帮助您实现所需。我的意思是,将出现一个对话框,单击“确定”/“是”/“等”将自动打开移动数据。类似于通过对话框提示您打开蓝牙,如果单击“是”,您的蓝牙将自动打开。或者打开谷歌地图显示“允许此应用使用您的位置?”并单击“是”将启用您的位置设置。不点击“是”并显示一个新的活动。我不确定谷歌是否真的允许这样做,作为一种安全功能。Mountain View的开发人员担心“轻松”打开数据可能会出错,并对用户收取不必要的费用。因此,他们确保将用户带到不同的屏幕上打开数据。打开3G后,您可以随时使用“上一步”按钮返回应用程序。如果我想在每个片段(换句话说,在每个UI页面)中检查互联网连接,该怎么办。让我们先说我点击图片,我看到了大图,但现在如果互联网连接丢失,我应该通知用户。在何处放置此代码,这样我就可以检查所有屏幕中的internet连接了吗?创建一个通用的UTI类并从那里访问它。您还可以创建广播并在任何地方收听网络更改。@Adnanhaider它不会进入该方法,而只是传递函数。@Pragnest Ghoda我复制您的代码并粘贴它。它显示连接错误detector@Adnanhaider1.首先确保您拥有清单中提到的“访问网络”和“状态”权限。2.创建一个返回true/false的方法,并将此方法粘贴到其中。(如果存在连接,则返回true或false)3。根据结果,确保继续执行流程。@Adnanhaider请注意。连接到WiFI并不意味着你总是连接到互联网。如果你连接到wifi,即使wifi没有连接互联网,它也会返回true。我按照所有这些步骤操作,但没有成功如果你的互联网已经连接,那么你能做什么