Search code examples
androidparse-platformnetwork-connection

Check mobile network data available in the android device


I developing an app with Parse in Android, and I need to check the internet connection on the device. So I did this for the login:

connectivityManager = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    networkInfo = connectivityManager.getActiveNetworkInfo();

btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (networkInfo != null && networkInfo.isConnected()) {
                if (validate()) {
                    ParseUser.logInInBackground(user, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            if (user != null) {
                                //code to send to MainActivity
                            } else {
                                //code if the values are wrong
                            }
                        }
                    });
                }
            }else {
                Toast.makeText(getActivity(), "Check your network connection", Toast.LENGTH_LONG).show();
            }
        }
    });

And here come my question. Here in my country our cellphone provider give us whatsapp and facebook for free but if you try to browse or use another app using your mobile data is not allowed. And when I try to login in my app without any mobile data, I dont know how, the app detected some mobile data and try to login. I was reading here in SO, best way to use network connection but I think those are not the solution. How can I check the megabytes or data in the device? or should I do something different to check the connection? Thanks in advance


Solution

  • http://clients3.google.com/generate_204 is used to check connection.

    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    ...
    private int inter = 0;   
    
    ...
    
    
    
    class checkconne extends AsyncTask<String, String, String> {
    
                            @Override
                            protected void onPreExecute() {
                                super.onPreExecute();
    
    
    
                            }
                            @Override
                            protected String doInBackground(String... args) {
    
                                int kk=0;
                                try {
                                    HttpURLConnection urlc = (HttpURLConnection)
                                            (new URL("http://clients3.google.com/generate_204")
                                                    .openConnection());
                                    urlc.setRequestProperty("User-Agent", "Android");
                                    urlc.setRequestProperty("Connection", "close");
                                    urlc.setConnectTimeout(1500);
                                    urlc.connect();
                                    kk= urlc.getResponseCode();
                                } catch (IOException e) {
    
    
        Log.e("qweqwe", "Error checking internet connection", e);
                            }
    
                            inter=kk;
    
    
    
                            return null;
                        }
                        @Override
                        protected void onPostExecute(String file_url) {
    
    
                            if (inter == 204){       
             Toast.makeText(MainActivity3.this, "is connected", Toast.LENGTH_LONG).show();             
    
                            }else{    
    
    
                                Toast.makeText(MainActivity3.this, "No connection", Toast.LENGTH_LONG).show();
    
                            }
    
    
                        }
                    }