Class control Android internet connection - Wifi-3G

Asked

Viewed 41 times

0

Hello.

Currently I control screen by screen the type of internet connection to which the user is connected or not.

I use a method similar to this:

    /*Check conection*/
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        //For 3G check
        final boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .isConnectedOrConnecting();
        //For WiFi Check
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .isConnectedOrConnecting();

        if (!is3g && !isWifi){

            Toast.makeText(AgendamentoActivity.this, "Sem conexão!! Verifique...", Toast.LENGTH_LONG).show();
            finish();

}

I would like to know how I could create a unique class for this and call in Activity I need to control this.

I don’t have much experience in this and I even found some models but I didn’t understand very well.

  • You want to create a model that when instantiated check that connection?

  • That’s right, so you don’t have to insert a repeat code snippet each time. @Guilhermecostamilam

1 answer

0


Just create a normal class with this method, and call in the constructor:

public class Conexao {
    private boolean wifi;
    private boolean movel;

    public Conexao() {
        verificarConexao();
    }

    public boolean getWifi() {
        return wifi;
    }

    public boolean getMovel() {
        return movel;
    }

    private void verificarConexao() {
       ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        //For 3G check
        movel = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        //For WiFi Check
        wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

        if (!is3g && !isWifi) {
            Toast.makeText(AgendamentoActivity.this, "Sem conexão!! Verifique...", Toast.LENGTH_LONG).show();
            finish();
        }
    }
}

When instating will be assigned the true or false to variables, then just pick up with the get

  • What method is this getSystemService()?

  • I don’t know, it was already in the question codes

  • Then, if you do not know, it is because this method does not belong to the class you wrote (Connected). So, how is it possible to be used there that way? This code will not compile.

  • I made an example assuming that the code given by the author worked only with that, if he did not put his complete code will not even work, but I will not create a new project in Android Studio (which is too slow), just to test a code that, according to the author, is working

  • The issue has nothing to do with his code. The problem is that, whatever the code, a class cannot thus use a method that is not stated in it. Note that my intention is not to "defect", it is only pedagogical.

  • I took a look and apparently the method getSystemService() is of class ConnectivityManager, if the code is correct

  • So, what is necessary to do so that this object can be used in the class Connected?

  • Import, do not import as not necessary

  • 1

    getSystemService() is a method of the Context class. In order to use the Context class Connection must receive a Context object in the constructor and store it in an attribute to be used when calling the method.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.