klinker41 / android-smsmms

Library for easily sending SMS and MMS for Android devices
678 stars 249 forks source link

Cannot receive MMS when data is disabled (solution included) #76

Open themelisx opened 7 years ago

themelisx commented 7 years ago

Even if app is system, fails to enable data to receive MMS. Here is the solution:

public void setMobileDataEnabled(Context context, boolean enabled) {

        if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pref_key_auto_data", true)) {
            if (Build.VERSION.SDK_INT >= 21) {
                try {
                    TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
                    if (null != setMobileDataEnabledMethod) {
                        setMobileDataEnabledMethod.invoke(telephonyService, enabled);
                    }
                    return;
                } catch (Exception ex) {
                    return;
                }
            } else {
                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);
                    return;
                } catch (NoSuchMethodException e) {
                    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    Class conmanClass;
                    try {
                        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());
                        Class[] cArg = new Class[2];
                        cArg[0] = String.class;
                        cArg[1] = Boolean.TYPE;
                        Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", cArg);
                        Object[] pArg = new Object[2];
                        pArg[0] = context.getPackageName();
                        pArg[1] = enabled;
                        setMobileDataEnabledMethod.setAccessible(true);
                        setMobileDataEnabledMethod.invoke(iConnectivityManager, pArg);
                        return;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        return;
                    }
                } catch (Exception e) {
                    return;
                }
            }
        }
    }
klinker24 commented 7 years ago

Are there any solutions that don't rely on reflection?

themelisx commented 7 years ago

This is a complete solution that works to all Android versions

klinker24 commented 7 years ago

I have tried this out, and it doesn't seem to work, at least on my Pixel. How are you testing and can you tell me the results you are getting?

06-14 16:24:59.994 13294-13294/com.klinker.android.send_message.sample W/System.err: java.lang.reflect.InvocationTargetException
06-14 16:24:59.995 13294-13294/com.klinker.android.send_message.sample W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-14 16:24:59.996 13294-13294/com.klinker.android.send_message.sample W/System.err:     at com.klinker.android.send_message.Utils.setMobileDataEnabled(Utils.java:223)
06-14 16:24:59.996 13294-13294/com.klinker.android.send_message.sample W/System.err:     at com.klinker.android.messaging_sample.MainActivity.onCreate(MainActivity.java:73)
06-14 16:24:59.996 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.Activity.performCreate(Activity.java:6982)
06-14 16:24:59.997 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
06-14 16:24:59.997 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
06-14 16:24:59.997 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
06-14 16:24:59.998 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
06-14 16:24:59.998 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
06-14 16:24:59.998 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
06-14 16:24:59.999 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.os.Looper.loop(Looper.java:164)
06-14 16:24:59.999 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6541)
06-14 16:24:59.999 13294-13294/com.klinker.android.send_message.sample W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-14 16:25:00.001 13294-13294/com.klinker.android.send_message.sample W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
06-14 16:25:00.001 13294-13294/com.klinker.android.send_message.sample W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err: Caused by: java.lang.SecurityException: No Carrier Privilege.
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.os.Parcel.readException(Parcel.java:1948)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.os.Parcel.readException(Parcel.java:1894)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:     at com.android.internal.telephony.ITelephony$Stub$Proxy.setDataEnabled(ITelephony.java:4094)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.telephony.TelephonyManager.setDataEnabled(TelephonyManager.java:5385)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:     at android.telephony.TelephonyManager.setDataEnabled(TelephonyManager.java:5375)
06-14 16:25:00.002 13294-13294/com.klinker.android.send_message.sample W/System.err:    ... 15 more
themelisx commented 7 years ago

Maybe at Android 6.+ works only if the app is at priv-app (system app). Check also the menifest permissions