android connect to internet event

后端 未结 4 751
情书的邮戳
情书的邮戳 2020-12-17 02:34

Is there a event which tells me that the device has connected to INTERNET (3G or wifi)? I need to start some request only after device connects to INTERNET. The code needs t

4条回答
  •  余生分开走
    2020-12-17 02:58

    public static boolean connectionCheck(final Context context)
    {   
        boolean returnTemp=true;
        ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conManager.getActiveNetworkInfo();
        if ((i == null)||(!i.isConnected())||(!i.isAvailable()))
        {
            AlertDialog.Builder dialog = new Builder(context);
            dialog.setTitle("CONNECTION STATUS");
            dialog.setMessage("Failed");
            dialog.setCancelable(false);
            dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            Toast.makeText(TennisAppActivity.mContext,"Wi-Fi On", Toast.LENGTH_LONG).show();
                }
            });
            dialog.show();
            return false;
        }
        return true;`enter code here`
    }
    

    using this function you are able to know device have internet connected of not. I hope this is help full to you.

提交回复
热议问题