I am writing a little app that only I will use and I want to pragmatically enable / disable my mobile data on a rooted android 4.5 device (I am running a custom Android L for Nexus 4).
I have looked for a while and I found the methods with reflection that worked until android 4.3.
I have also seen the method from this post Toggle mobile data programmatically on Android 4.4.2 but this requires cyanogenmod.
From what I can find on the internet this is impossible for non-root apps but my question is:
is there something I can do with my root privileges to accomplish this?
I've created this method looking around on internet; it works fine on rooted android 5.0.1
Basically you have to pass true if you want the connection to be enabled, false otherwise, and the context of your application.
private final static String COMMAND_L_ON = "svc data enable\n ";
private final static String COMMAND_L_OFF = "svc data disable\n ";
private final static String COMMAND_SU = "su";
public static void setConnection(boolean enable,Context context){
String command;
if(enable)
command = COMMAND_L_ON;
else
command = COMMAND_L_OFF;
try{
Process su = Runtime.getRuntime().exec(COMMAND_SU);
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes(command);
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
Please report if this has problems on some device.
EDIT: Now also compatible with android 5.1 Credit
Use this
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = tm.class.getDeclaredMethod( "setDataEnabled", boolean.class);
methodSet.invoke(tm, true);
Edit:
This requires permission MODIFY_PHONE_STATE, this is System or signature level permission.
Ideally you could create a runnable jar file with this code and execute it using
export CLASSPATH=<jar path>
exec app_process <jar-dir-path> your.package.name.classname "$#"
from su shell.
I noticed that the service call method does not work consistently on all devices. The number to be used in it varied from device to device.
I have found the following solution which works without any issue across all ROOTED devices.
Simply execute the following via su
To enable mobile data
svc data enable
To disable mobile data
svc data disable
It's as simple as that.
void turnData(boolean ON) throws Exception
{
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if(currentapiVersion == Build.VERSION_CODES.FROYO)
{
Log.i("version:", "Found Froyo");
try{
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) cx.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
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 (ON) {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
}catch(Exception e){
Log.e("Error:",e.toString());
}
}
else
{
Log.i("version:", "Found Gingerbread+");
final ConnectivityManager conman = (ConnectivityManager) cx.getApplicationContext().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, ON);
}
}
Related
I'm trying to enable/disable data mobile in specific moments. I've tried it on MotoG 4.4.2 (Rooted) with it I had no problem to do this, but when I've tried it on Samsung Galaxy Grand Prime 4.4.4 (No Root) it doesn't work, also I've tried it on Samsung Galaxy S3 4.3 (No Root) but it worked.
MotoG Worked -- WHY? -- ROOT Acces
Samsung Galaxy Grand Prime didn't work -- WHY? -- I think it doesn't work because when I try to do it manually I've to accept a dialog that I want to lose data connectivity, and I think this is the problem because it doesn't work.
Samsung Galaxy S3 worked -- WHY? -- I think it worked because the user put "Don't show me again" and user accepted it before.
The code that I'm trying is :
public void GetDataConnectionAPI() {
this.context.getApplicationContext();
TelephonyManager telephonyManager =
(TelephonyManager) this.context.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) {
e.printStackTrace();
}
}
And then I enable/disable the data with this method :
public void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
When I want to Enable I call this method :
public void EnableMobileData(){
try {
setMobileDataEnabled(context, true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
On my Manifest I put
<!-- 3g uses-permisions-->
<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" />
I wonder if it's any possibility to avoid this dialog or just accept it programmatically..
I've read Bojan Kogoj answer that sais that I've to :
Pre-installed into a system folder on the ROM
Compiled by a manufacturer using their security certificate
But I'm wondering if is any other way to do this.
The MODIFY_PHONE_STATE permission is not guaranteed to work in all cases. By default it is a system-level permission, so it will only work on devices whose manufacturers have enabled it.
Similarly, methods like getITelephony() are device/manufacturer-specific, and there is no way to get the name of the methods for doing so on every device.
Unfortunately there is no guaranteed way to do this.
I'm having android(OS_VERSION 4.0) device. I would like to share the files to another android device through the wifi networks. I know, This can be done through wifi p2p(WifiDirect) in android 4.0 above. But this is not possible in android 2.3.3 devices(Prior to Android 4.0). I found the Superbeam application does the file sharing through shared networks in android 2.3.3.This application create the wifi tethering without sharing the internet connection of the device. The created tethering is only used for sharing the files not for sharing the internet. How to achieve this concept. Can anyone help me?
This answer may help to someone having the same question. The simple logic i implemented is,
1.Create the wifi tethering(Hotspot)
2.Disable the mobile data connection
Code is,
//To enable the wifi hotspot
setWifiTetheringEnabled(true);
//To disable the mobile data cnnection
setMobileDataEnabled(false);
private void setWifiTetheringEnabled(boolean enable) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
try {
method.invoke(wifiManager, null, enable);
} catch (Exception ex) {
}
break;
}
}
}
private void setMobileDataEnabled(Context context, boolean enabled) {
try {
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);
} catch (ClassNotFoundException | NoSuchFieldException
| IllegalAccessException | IllegalArgumentException
| NoSuchMethodException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm trying to Enable / Disable Mobile Data Connexion.
I've used this code by rIHaN JiTHiN (Enable/Disable Mobile Data (GPRS) using code) and it's works perfectly on Android 4.0, but it's doesn't on my Galaxy S (Froyo 2.2)...
Is there a way to enable / disable data connexion programmatically ?
If anyone had any idea why it's doesn't work on Froyo, would be really helpful. According to rIHaN JiTHiN, this code works on all Android version...
You can check whether it is enabled or disabled by using below code
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile.isConnected()) {
//if internet connected
}
if it is disabled, you can enable it on your froyo device by using this one
void turnData(boolean ON) throws Exception
{
if(bv == Build.VERSION_CODES.FROYO)
{
Log.i("version:", "Found Froyo");
try{
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
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 (ON) {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
}catch(Exception e){
Log.e("Error:",e.toString());
}
}
else
{
Log.i("version:", "Found Gingerbread+");
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().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, ON);
}
and also dont forget to add these to manifest
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.MODIFY_PHONE_STATE
android.permission.READ_PHONE_STATE
Duplicate question - How to get my wifi hotspot ssid in my current android system
Sorry for duplicate this qustion, but it still doesn't have the answer. My mobile in tethering mode, so I want to know SSID of it. How can I find this one? Thanks a lot!
It's a bit late but i recently managed to get the SSID of the device's hotspot. It's working on my Galaxy Nexus, but haven't tested it quite much.
public static WifiConfiguration getWifiApConfiguration(final Context ctx) {
final WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
final Method m = getWifiManagerMethod("getWifiApConfiguration", wifiManager);
if(m != null) {
try {
return (WifiConfiguration) m.invoke(wifiManager);
} catch(Exception e) {
}
}
return null;
}
private static Method getWifiManagerMethod(final String methodName, final WifiManager wifiManager) {
final Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
return method;
}
}
return null;
}
Just call getWifiApConfiguration(getActivity()).SSID to get the hotspot name. Nullpointer check is recommended before ;)
WifiManager mng = (WifiManager)context.getSystemService(Context.WIFI_SERVICE).
String currentSSID = mng.getConnectionInfo().getSSID()
I have HTC Flyer tab with version Android 2.3.4. I am not able to retrieve the IMEI number through TelephonyManager.getDeviceId(). It always return null.
Can somebody try to read out the IMEI on another device. I would like to know whether it is a Google or HTC problem.
This is a 'GSM' device. And it is brand new, didn't have any OS update.
FYI,
Included Manifest:
My programs on sumsung galaxy, Motorola xoom, and all smartphones working well.
Some of the tab devices do not have IMEI number. You can get WI-FI MAC address of the device.
WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String ID = wifiInf.getMacAddress();
if you are getting null with TelephonyManager.getDeviceId(), use
Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
for example:
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
AndroidDeviceId = mTelephony.getDeviceId();
}
else{
AndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
even ANDROID_ID is not secure to use:
More specifically, Settings.Secure.ANDROID_ID. This is a 64-bit
quantity that is generated and stored when the device first boots. It
is reset when the device is wiped.
ANDROID_ID seems a good choice for a unique device identifier. There
are downsides: First, it is not 100% reliable on releases of Android
prior to 2.2 (“Froyo”). Also, there has been at least one
widely-observed bug in a popular handset from a major manufacturer,
where every instance has the same ANDROID_ID.
but i recommed use this method suggested in Android Developer´s Blog: Identifying App Installations:
public class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists())
writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
out.close();
}
}
#SuppressWarnings("rawtypes")
Class SystemProperties = null;
SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
#SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
Method get=null;
get = SystemProperties.getMethod("get", paramTypes);
//Parameters
Object[] params = new Object[1];
params[0] = new String("ro.gsm.imei");
IMEI = (String) get.invoke(SystemProperties, params);