首页 > 解决方案 > TelephonyManager 连接到数据网络

问题描述

我的问题是关于 Android 工作室的。我正在尝试实现该方法:setNetworkSelectionModeManual来自TelephonyManager库,但我没有取得任何成功。

每当它被调用时,应用程序就会崩溃。这可能是一个许可的事情,希望有人能够提供帮助吗?

编码:

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();

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

标签: androidtelephonymanager

解决方案


问题是该应用程序仅适用于 Android 9 或更高版本的设备 [Android P: API: 28] 由于此方法调用 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 

setNetworkSelectionModeManual 方法仅在 API 级别 28 [Android P / Android 9] 中添加

在API 级别 28中添加的 setNetworkSelectionModeManual public boolean setNetworkSelectionModeManual (String operatorNumeric, boolean persistSelection) 要求无线电连接到输入网络并将选择模式更改为手动。

需要权限:MODIFY_PHONE_STATE 或调用应用程序具有运营商权限(请参阅 hasCarrierPrivileges())。

安卓版本历史

我看不到所有相关代码,但在您的活动中存在调用该方法或执行导致调用该方法的电话相关任务的某些内容。

解决方案:

  • 不要调用方法
  • 将 gradle 文件中的最小 API 设置为 28 以防止旧设备运行该应用
  • 在 Android P 模拟器上运行您的应用
  • 有时还会在兼容性库中添加新的 API
  • 仅当 API 级别足够高时才运行代码

样本:

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
}

推荐阅读