Nabukodonosor
Nabukodonosor

Reputation: 699

How to check if GPRS/3G is on?

I want to check, when my app is starting, if GPRS/3G is on or off. I have this code below, and it checks generally if internet is on (wifi or 3G) and if for example wifi is on it sets my both wifi AND gprs toggle buttons ON. And I can't have that. I want if wifi is on to turn on only wifi toggle button, not the gprs. Buttons works fine, only checking and setting it's values on startup does not work.

public class SwarmPopup extends SwarmActivity implements OnClickListener{

    private boolean isNetworkConnected() {
          ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo ni = cm.getActiveNetworkInfo();
          if (ni == null) {
           // There are no active networks.
           return false;
          } else
           return true;
         }


    Button ok;
    WifiManager wifiMan;
    ToggleButton wifi, gprs;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.swarmpopup);

        addListenerOnButton();

        wifi.setChecked(wifiMan.isWifiEnabled());

        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        wifi.setOnClickListener(new OnClickListener() {  //Ukljucuje WiFi

            @Override
            public void onClick(View v) {

                wifi.setEnabled(wifiMan.isWifiEnabled());

                try
                  {
                    if (((ToggleButton)v).isChecked()){
                    SwarmPopup.this.wifiMan.setWifiEnabled(true);
                    Toast.makeText(getApplicationContext(), "WiFi je uključen", Toast.LENGTH_LONG).show();
                  }else{
                        SwarmPopup.this.wifiMan.setWifiEnabled(false);
                    Toast.makeText(getApplicationContext(), "WiFi je isključen", Toast.LENGTH_LONG).show();
                  }
                  }
                catch (Exception localException)
                  {
                    Log.e("SwarmPopup", "error on WiFi listerner: " + localException.getMessage(), localException);
                  }
            }
        });

        gprs.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try
                  {
                    if (((ToggleButton)v).isChecked()){
                      GprsSettings.setMobileDataEnabled(getApplicationContext(), true);
                    Toast.makeText(getApplicationContext(), "GPRS je uključen", Toast.LENGTH_SHORT).show();
                  }else{
                      GprsSettings.setMobileDataEnabled(getApplicationContext(), false);
                    Toast.makeText(getApplicationContext(), "GPRS je isključen", Toast.LENGTH_SHORT).show();
                  }
                  }
                catch (Exception localException)
                  {
                    Log.e("SwarmPopup", "error on GPRS listerner: " + localException.getMessage(), localException);
                  }
            }
        });

        gprs.setChecked(isNetworkConnected());

    }

    private void addListenerOnButton() {
        Typeface dugme = Typeface.createFromAsset(getAssets(), "myriad.ttf");
        ok = (Button) findViewById(R.id.btOKSwarm);
        ok.setTypeface(dugme);
        gprs = (ToggleButton) findViewById(R.id.tbGPRS);
        wifi = (ToggleButton) findViewById(R.id.tbWiFi);

        // Getting the WiFi Services
        wifiMan=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        //Proverava da li je WiFi ukljucen ili ne


    }

    @Override
    public void onClick(View v) {

    }

}

Upvotes: 0

Views: 891

Answers (1)

cwhsu
cwhsu

Reputation: 1523

You need to use reflection to check if mobile is currently enabled or not because there is no public API for get/set mobile data.

see this thread. How to check if mobile network is enabled/disabled

Upvotes: 1

Related Questions