user3420917
user3420917

Reputation: 19

TelephonyManager connect to data networks

my question is about Android studio. I am trying to implement the method: setNetworkSelectionModeManual from the TelephonyManager library, but I haven't had any success.

Whenever it is getting called, the app crashes. It is probably a permission thing, would appreciate anyone be able to help?

The code:

public void startTimer(){
    countDownTimer = new CountDownTimer(timeLeftInMillisecond,1000) {
        @Override
        public void onTick(long l) {
            timeLeftInMillisecond = l;
            updateTimer();
        }

        @Override
        public void onFinish() {
            //switching to a different network by mpln
            boolean networkChanged = tm.setNetworkSelectionModeManual("USAW6", false);
                //restart timer
            countDownTimer.start();
        }
    }.start();

The error in logcat:

07-22 18:14:04.941 27289-27310/com.example.yakir.webbing_hlr E/OpenGLRenderer: allen debug liyu Key: 0
07-22 18:14:04.944 27289-27310/com.example.yakir.webbing_hlr E/OpenGLRenderer: allen debug liyu Key: 34359738371
07-22 18:14:04.945 27289-27310/com.example.yakir.webbing_hlr E/OpenGLRenderer: allen debug liyu Key: 240518168576
07-22 18:14:04.946 27289-27310/com.example.yakir.webbing_hlr E/OpenGLRenderer: allen debug liyu Key: 68724719680
07-22 18:14:08.062 27289-27289/com.example.yakir.webbing_hlr I/hwaps: JNI_OnLoad
07-22 18:14:08.110 27289-27310/com.example.yakir.webbing_hlr E/OpenGLRenderer: allen debug liyu Key: 103084458052
07-22 18:14:18.107 27289-27289/com.example.yakir.webbing_hlr E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.yakir.webbing_hlr, PID: 27289
    java.lang.NoSuchMethodError: No virtual method setNetworkSelectionModeManual(Ljava/lang/String;Z)Z in class Landroid/telephony/TelephonyManager; or its super classes (declaration of 'android.telephony.TelephonyManager' appears in /system/framework/framework.jar:classes2.dex)
        at com.example.yakir.webbing_hlr.MainActivity$2.onFinish(MainActivity.java:74)
        at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
07-22 18:14:18.158 27289-27289/com.example.yakir.webbing_hlr I/Process: Sending signal. PID: 27289 SIG: 9

Upvotes: 0

Views: 946

Answers (1)

Elletlar
Elletlar

Reputation: 3234

The problem is that the app only works on Android 9 or above devices [Android P: API: 28] due to this method call setNetworkSelectionModeManual()

setNetworkSelectionModeManual(Ljava/lang/String;Z)Z in class 
Landroid/telephony/TelephonyManager; or its super classes (declaration of 
'android.telephony.TelephonyManager' appears in 
/system/framework/framework.jar:classes2.dex) at 
com.example.yakir.webbing_hlr.MainActivity$2.onFinish(MainActivity.java:74) at 

The setNetworkSelectionModeManual method was only added in API level 28 [Android P / Android 9]

setNetworkSelectionModeManual added in API level 28 public boolean setNetworkSelectionModeManual (String operatorNumeric, boolean persistSelection) Ask the radio to connect to the input network and change selection mode to manual.

Requires Permission: MODIFY_PHONE_STATE or that the calling app has carrier privileges (see hasCarrierPrivileges()).

Android Version History

I cannot see all the relevant code, but there is something in your activity either calling that method or doing a telephony related task that causes that method to be called.

Solutions:

  • Do not call the method
  • Set the minimum API in the gradle file to 28 to prevent old devices running the app
  • Run your app on the Android P emulator
  • Sometimes new APIs are added the compatibility libraries as well
  • Only run the code if the API level is high enough

Sample:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { // or 28
   // Call the method
} else {
   // Call different methods, possibly deprecated ones that do the same thing
}

Upvotes: 1

Related Questions