2012-09-21 1 views

Répondre

52

Il n'existe aucun moyen officiel de le faire. Cependant, il peut être réalisé officieusement avec la réflexion.

Pour Android 2.3 et au-dessus:

private void setMobileDataEnabled(Context context, boolean enabled) { 
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final Class conmanClass = Class.forName(conman.getClass().getName()); 
    final 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); 
} 

Cela nécessite également l'autorisation suivante.

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

Pour Android 2.2 et ci-dessous:

Method dataConnSwitchmethod; 
Class telephonyManagerClass; 
Object ITelephonyStub; 
Class ITelephonyClass; 

TelephonyManager telephonyManager = (TelephonyManager) context 
     .getSystemService(Context.TELEPHONY_SERVICE); 

if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){ 
    isEnabled = true; 
}else{ 
    isEnabled = false; 
} 

telephonyManagerClass = Class.forName(telephonyManager.getClass().getName()); 
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); 
getITelephonyMethod.setAccessible(true); 
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); 
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName()); 

if (isEnabled) { 
    dataConnSwitchmethod = ITelephonyClass 
      .getDeclaredMethod("disableDataConnectivity"); 
} else { 
    dataConnSwitchmethod = ITelephonyClass 
      .getDeclaredMethod("enableDataConnectivity"); 
} 
dataConnSwitchmethod.setAccessible(true); 
dataConnSwitchmethod.invoke(ITelephonyStub); 

Cela a nécessité l'autorisation suivante:

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

Notez que ces deux sont non officiels et peuvent ne plus fonctionner. Plus aucune preuve de ce genre de chose ne devrait être nécessaire, car la méthode 2.2 et ci-dessous a éclaté sur 2.3.

+0

Merci beaucoup! ça marche :) – chuyitox

+0

Hey Raghav, dites-moi quels paquets importés dans le projet pour 2.3? Quand j'importe java.lang.reflect.Method; J'ai une erreur. –

+0

Puis-je utiliser votre code comme private void setMobileDataEnabled (booléen activé) {Context context; // ...}? En d'autres termes, pourquoi choisissez-vous d'utiliser Contexte comme argument? –

2

Je suis toujours sur 2.1, donc cette solution fonctionne pour moi.

Mais vous devez également inclure la permision MODIFY_PHONE_STATE.

code complet (avec un bouton toogle) 2.1:

package com.rivaldo.turn3gonoff; 

import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.telephony.TelephonyManager; 
import android.view.Menu; 
import android.widget.CompoundButton; 
import android.widget.ToggleButton; 

public class Turn3GOnOff extends Activity { 

Method dataConnSwitchmethod_ON; 
Method dataConnSwitchmethod_OFF; 
Class telephonyManagerClass; 
Object ITelephonyStub; 
Class ITelephonyClass; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_turn3_gon_off); 

    GetDataConnectionAPI(); 

    ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton); 
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       turn3GOn(); 
      } else { 
       turn3GOff(); 
      } 
     } 
    }); 

} 

private void GetDataConnectionAPI() { 
    this.getApplicationContext(); 
    TelephonyManager telephonyManager = 
        (TelephonyManager) this.getApplicationContext(). 
               getSystemService(Context.TELEPHONY_SERVICE); 

    try { 
     telephonyManagerClass = Class.forName(telephonyManager.getClass().getName()); 
     Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); 
     getITelephonyMethod.setAccessible(true); 
     ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); 
     ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName()); 

     dataConnSwitchmethod_OFF = 
         ITelephonyClass.getDeclaredMethod("disableDataConnectivity"); 
     dataConnSwitchmethod_ON = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); 
    } catch (Exception e) { // ugly but works for me 
     e.printStackTrace(); 
    } 
} 
private void turn3GOn() { 
    dataConnSwitchmethod_ON.setAccessible(true); 
    try { 
     dataConnSwitchmethod_ON.invoke(ITelephonyStub); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
} 

private void turn3GOff() { 
    dataConnSwitchmethod_OFF.setAccessible(true); 
    try { 
     dataConnSwitchmethod_OFF.invoke(ITelephonyStub); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
} 

} 

Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.rivaldo.turn3gonoff" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="7" /> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.rivaldo.turn3gonoff.Turn3GOnOff" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


</application> 
</manifest> 

activity_turn3_gon_off.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context=".Turn3GOnOff" > 

<ToggleButton 
    android:id="@+id/toggleButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="60dp" 
    android:textOn="3G on" 
    android:textOff="3G off" 
    android:text="ToggleButton" /> 

</RelativeLayout> 
4
public void onClick(View view){ 
     ConnectivityManager dataManager; 
     dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
     Method dataMtd = null; 
     try { 
      dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); 
     } catch (NoSuchMethodException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     dataMtd.setAccessible(true); 
     try { 
      dataMtd.invoke(dataManager, true); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
+1

Ne fonctionne plus! –

+0

+1, il active les données mobiles sur Android 4.3; mais nous devons ajouter l'autorisation suggérée par la réponse précédente Alberici

5

Surrou trouver le code avec try/catch blocs

public void mobiledataenable(boolean enabled) { 

try { 
     final ConnectivityManager conman = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
     final Class<?> conmanClass = Class.forName(conman.getClass().getName()); 
     final 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); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    }  
} 

Dans le Manifest, ajoutez l'autorisation suivante:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
Questions connexes