0

I want to enable the mobile network/data-network programatically. For this I am using the code given below but its not working. I didn`t receive any error. Toast pops up and says that "Mobile Network has been enabled" but its actually not enabled. Please any help.

Here is complete code.

public class MainActivity extends Activity {
ConnectivityManager connectivity;
NetworkInfo wifiNetworkInfo, mobileNetworkInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connectivity  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    wifiNetworkInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    mobileNetworkInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifiNetworkInfo.isConnected())
        Toast.makeText(this, "WiFi is connected", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "WiFi is not connected", Toast.LENGTH_LONG).show();
    if(mobileNetworkInfo.isConnected())
        Toast.makeText(this, "MObileNetwork is connected", Toast.LENGTH_LONG).show();
    else{
        boolean enabled = false;
        try {
            setMobileDataEnabled(getBaseContext(), enabled);
            Toast.makeText(this, "MObileNetwork has been Enabled", Toast.LENGTH_LONG).show();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}}
private void setMobileDataEnabled(Context context, boolean enabled) throws Throwable {
       final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
       final Class<?> conmanClass = Class.forName(conman.getClass().getName());
       final java.lang.reflect.Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
       iConnectivityManagerField.setAccessible(true);
       final Object iConnectivityManager = iConnectivityManagerField.get(conman);
       final Class<?> iConnectivityManagerClass =  Class.forName(iConnectivityManager.getClass().getName());
       final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
       setMobileDataEnabledMethod.setAccessible(true);
       setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

Rida Shahid
  • 386
  • 7
  • 22

2 Answers2

1

I had this problem in my last project and the approach we had to end up using, also according to Google's way to handle this issue was to pop up the Setting->Mobile Networks Activity in front of the user instead, so he could explicitly enable it, not only for requirement reasons but also for Legal Term issues because if your app consumes 3G data making the user spend money without his knowledge, you can get in legal problems...

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • yes @Martin you are right regarding the legal terms. But this is just I want for testing purpose only. – Rida Shahid Jul 23 '13 at 20:59
  • Ok, in that case, what i did to be able to enable it was rooting the phone and removing the security constraints, i don't know if you are getting a SecurityException for trying to hit the hidden method using reflection, but any way you won't be able to do it programatically because phone vendors usually do not allow it, hence you have to do it on a rooted phone... – Martin Cazares Jul 23 '13 at 21:03
0

EDIT: The ConnectivityService in the SystemService does other Access control checks when the method setMobileDataEnabled is called

/**
 * @see ConnectivityManager#setMobileDataEnabled(boolean)
 */
public void setMobileDataEnabled(boolean enabled) {
    enforceChangePermission();
    if (DBG) log("setMobileDataEnabled(" + enabled + ")");

    mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
            (enabled ? ENABLED : DISABLED), 0));
}

private void enforceChangePermission() {
    mContext.enforceCallingOrSelfPermission(
            android.Manifest.permission.CHANGE_NETWORK_STATE,
            "ConnectivityService");
}

It looks like your app needs the CHANGE_NETWORK_STATE permission or you would probably have to run your app as system or a user with elevated priviledges.

dudebrobro
  • 1,287
  • 10
  • 17