1

I am an android developer, I want to run 3G connection on or off via a button in my application.

Mike
  • 2,132
  • 3
  • 20
  • 33
user2207848
  • 47
  • 3
  • 9
  • That thread doesn't provide an up-to-date answer. Solutions given there stopped working after 2.2 and/or require root access. – ohaleck May 14 '13 at 20:25
  • Added a description of a possible workaround as an answer below. It works fine in one of my apps. – ohaleck May 14 '13 at 20:39

1 Answers1

1

I've done an extensive search on this topic recently and must tell there is no simple solution to this problem on modern Android versions, unless you run your app as root.

I found a simple, yet a bit limited, workaround though: 3G connectivity is active only if WiFi is down, so each time you are about to initialize a network connection, you must check if WiFi is up and connected. Below is some code for your sample activity:

public class MyActivity extends Activity {
    boolean enable3g = false;

    public void onCreate() {
        setContentView(R.layout.my_layout);
        Button enable3gButton = (Button) findViewById(R.id.button3g);
        enable3gButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MyActivity.this.enable3g = !MyActivity.this.enable3g;
            }
        });

        if (enable3g || isWifiConnected()) {
            // do your stuff that requires network access here
        }



    }
    public boolean isWifiConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    }
}

In AndroidManifest you need ACCESS_NETWORK_STATE permission (and a declaration of your activity of course):

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application .....>
    <activity android:name="MyActivity" />

If WiFi is not connected (the isWifiConnected() method returns false), do not start to connect. An existing TCP connection cannot "jump" from one network to another so it will just time out if WiFi connectivity is broken, making the solution quite safe.

The limitation is, that you must be able to control your application's connections. If you are using WebView, or some 3rd-party piece of software, it may get tricky.

ohaleck
  • 661
  • 4
  • 20
  • my objectif is to create a button, and user can turn on the internet from it ! i didn't find a solution – user2207848 May 14 '13 at 20:44
  • I have expanded the example to include a sample activity. It assumes you have defined a button in a layout file `my_layout.xml` and the id of the button is `button3g`. You need to wrap all the code that requires network in simple `if` blocks, that check that either `enable3g` is set to `true`, which is done by means of your button, or that WiFi is available. – ohaleck May 14 '13 at 20:56
  • should i add some thing in the manifest ? – user2207848 May 14 '13 at 21:26
  • Yes, you need the `ACCESS_NETWORK_STATE` permission (sorry I forgot to add it earlier) and of course the declaration of your activity. Edited the answer. – ohaleck May 14 '13 at 21:35
  • Could you please upvote the answer if you find it useful? – ohaleck May 14 '13 at 22:13
  • i am sorry but it doesn t work :( – user2207848 May 14 '13 at 22:34
  • Can you describe the problem? As I wrote, it's a workaround. You shouldn't expect the 3G to get disabled. It should just prevent your app from using it. – ohaleck May 14 '13 at 23:25