0

当我的应用程序启动时,我想检查 GPRS/3G 是打开还是关闭。我在下面有这段代码,它通常检查互联网是否打开(wifi 或 3G),例如是否打开了 wifi,它将我的 wifi 和 gprs 切换按钮都设置为 ON。而我不能拥有那个。如果wifi打开,我希望只打开wifi切换按钮,而不是gprs。按钮工作正常,仅在启动时检查和设置其值不起作用。

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) {

    }

}
4

1 回答 1

1

您需要使用反射来检查当前是否启用了移动设备,因为没有用于获取/设置移动数据的公共 API 。

看到这个线程。 如何检查移动网络是否启用/禁用

于 2013-03-14T14:00:39.310 回答