1

I need to get access to the Data Roaming Status on Moto Device(5.0.1)

if (Settings.Secure.getInt(context.getContentResolver(),Settings.Secure.DATA_ROAMING) == 1) {
    //Data Roaming Enabled      
    flag = true;
} else {
    // Data Roaming Disabled
    flag = false;
}

I found problem with this when using a Motorola device. Secure Settings in this device are found in android.provider.MotorolaSettings.Secure where as in other devices it's android.provider.Settings.Secure.

Is there a way to resolve this or any other way to get roaming status?

Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67
aman arora
  • 263
  • 5
  • 16
  • You could use reflection to test if the class exists : [see](http://stackoverflow.com/questions/3466568/check-if-class-exists-in-java-classpath-without-running-its-static-initializer) – Hacketo Jun 08 '15 at 13:07
  • How can i use it? Please help me on same... – aman arora Jun 08 '15 at 13:34
  • I tried using this Settings.Secure.getInt(context.getContentResolver(),"android.provider.MotorolaSettings.Secure.DATA_ROAMING") but nothing happens... – aman arora Jun 08 '15 at 13:40
  • this could be useful : http://stackoverflow.com/questions/2373779/roaming-detection-in-android – Hacketo Jun 09 '15 at 09:46

1 Answers1

1

One solution here, use reflection to check if Motorola classes are availables. If they're not here, you need to use the default api, then call getInt on the available system.

Not able to test it on a Motorola device.

public static boolean isEnabled(Context context){

    Class<?> baseSettingsClass = null;

    // Retrieve the 'default' settings api
    try {
        if (android.os.Build.VERSION.SDK_INT >= 17){
            baseSettingsClass = Class.forName( "android.provider.Settings$Global");
        }
        else{
            baseSettingsClass = Class.forName( "android.provider.Settings$Secure" );
        }
    }catch(Exception e){}

    Class<?> secureClass = null;


    // Try retrieve the motorola class
    try{
        secureClass = Class.forName("com.motorola.android.provider.MotorolaSettings$Secure" );
    }catch(Exception e){}


    // If it failed, use the 'default' api class
    if (secureClass == null){
        if (baseSettingsClass != null){
            secureClass = baseSettingsClass;
        }
        else{
            return false;
        }
    }

    try {
        // Retrieve the getInt method
        Method getIntMethod = secureClass.getDeclaredMethod("getInt", ContentResolver.class, String.class);

        // Execute getInt(context.getContentResolver(), Settings.Secure.DATA_ROAMING)
        int result = (Integer) (getIntMethod.invoke(null, context.getContentResolver(), (String)baseSettingsClass.getField("DATA_ROAMING").get(null)));

        return  result == 1;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}
Hacketo
  • 4,978
  • 4
  • 19
  • 34
  • class not found : android.provider.MotorolaSettings$Secure and it returns false even when data roaming is enabled in settings – aman arora Jun 09 '15 at 07:27
  • Class android.provider.Settings$Global has been found but always return false. – aman arora Jun 09 '15 at 07:32
  • I don't know Motorola, I used the class you mentioned in your post.. Is your basic code working ? – Hacketo Jun 09 '15 at 07:33
  • Its working for other devices but for Moto Devices its not working. Checked in Moto G(gen 2) OS 5.0.2. – aman arora Jun 09 '15 at 07:35
  • can you try with to replace motorola class with this class instead : `Class.forName("com.motorola.android.provider.MotorolaSettings");` – Hacketo Jun 09 '15 at 07:37
  • Using this class found, But java.lang.reflect.InvocationTargetException occurs at this line : int result = (Integer) (getIntMethod.invoke(null, context.getContentResolver(), (String) dataroamingField.get(null))); – aman arora Jun 09 '15 at 08:02
  • I found problem in logs : W/MotorolaSettings(28328): no such table to get this name = data_roaming – aman arora Jun 09 '15 at 08:31
  • and what about this one `Class.forName("com.motorola.android.provider.MotorolaSettings$Secure");` ? (I can't find it on the web..) – Hacketo Jun 09 '15 at 08:50
  • Don't know may be motorola has added its api's over secure settings android, thats why it always return false – aman arora Jun 09 '15 at 08:53
  • Is there any other way to get DataRoaming Status without using Secure settings? – aman arora Jun 09 '15 at 09:01
  • I found some things, I edit my answer. (not able to test code for moto devices) – Hacketo Jun 09 '15 at 09:34
  • Where did you see that you could access `android.provider.MotorolaSettings.Secure` to retrieve this info ? I can't find a class with this name. – Hacketo Jun 09 '15 at 09:39
  • I found that in logcat, in most devices I get android.provider.Settings.Secure, where as in motorola device i get android.provider.MotorolaSettings.Secure – aman arora Jun 09 '15 at 10:49
  • W/MotorolaSettings(786):no such table to get this name = vm_vvm_roaming_selection W/System.err(786):java.lang.Exception W/System.err(786):at com.motorola.android.provider.MotorolaSettings.getString(MotorolaSettings.java:290) W/System.err(786):at com.motorola.android.provider.MotorolaSettings.getInt(MotorolaSettings.java:339) W/System.err(786):at java.lang.reflect.Method.invoke(Method.java:372) E/MyTest(786):FailedToGetResult: java.lang.reflect.InvocationTargetException – aman arora Jun 09 '15 at 13:28