简体   繁体   English

PhoneStateListener 超出了 Android 11 上允许的注册侦听器数量

[英]PhoneStateListener is exceeding the number of permissible registered listeners on Android 11

I have an Android app that includes a background service (that typically launches at device startup) and an activity that provides a UI.我有一个 Android 应用程序,其中包含一个后台服务(通常在设备启动时启动)和一个提供 UI 的活动。 Occasionally, on Android 11 devices, the background service is throwing the following exception when trying to launch the activity:有时,在 Android 11 设备上,后台服务在尝试启动 Activity 时会抛出以下异常:

java.lang.RuntimeException: Unable to start service com.me.myapp.MyService@97e474c with Intent { cmp=com.me.myapp/.MyService }: java.lang.IllegalStateException: Pid 12345 has exceeded the number of permissible registered listeners. Ignoring request to add.

The app is compiled with API 30, targeting API 28. If the user requests to shut down the background service, listeners are unregistered at that time.该应用程序使用 API 30 编译,面向 API 28。如果用户请求关闭后台服务,则此时未注册侦听器。 It has tens of thousands of installs and has been in circulation for several years with no changes to the pertinent code:它有数以万计的安装,并且已经流通了好几年,相关代码没有改变:

public int onStartCommand(Intent in, int flags, int startId) {
    super.onStartCommand(in, flags, startId);
    signalStrengthListener = new SignalStrengthListener();

    // **the line below is flagged as causing the exception**
    ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener,
        SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS | SignalStrengthListener.LISTEN_SERVICE_STATE |
        SignalStrengthListener.LISTEN_CALL_STATE | SignalStrengthListener.LISTEN_DATA_ACTIVITY |
        SignalStrengthListener.LISTEN_DATA_CONNECTION_STATE);

I am unable to identify exactly what circumstances trigger this exception other than it appears to be limited to Android 11, and only occurs if the background service has been running for awhile (ie not when launching the app from scratch).除了似乎仅限于 Android 11 之外,我无法确定究竟是什么情况触发了此异常,并且仅在后台服务运行了一段时间时才会发生(即不是在从头启动应用程序时)。 The service does not register any other ongoing listeners.该服务不会注册任何其他正在进行的侦听器。 It appears that some behavior has changed with API 30, but I cannot find any references to it. API 30 似乎改变了某些行为,但我找不到任何对它的引用。 How can I resolve this issue?我该如何解决这个问题?

Perhaps this code should be in onCreate() , not onStartCommand() , since the latter can be called more than once in the lifetime of the service.也许这段代码应该在onCreate() ,而不是onStartCommand() ,因为后者在服务的生命周期中可以被调用多次。

Moving the initialization of the SignalStrengthListener constructor to onCreate() did indeed resolve the issue.SignalStrengthListener构造函数的初始化移动到onCreate()确实解决了这个问题。 CommonsWare correctly noted that onStartCommand() can be called more than once, which eventually led to the exception. CommonsWare 正确地指出onStartCommand()可以被多次调用,这最终导致了异常。

It should be noted that my implementation of the .listen method did not function properly when moved out of onStartCommand() .应该注意的是,当移出onStartCommand()时,我的.listen方法实现无法正常运行。

I fixed it by canceling the listener like this:我通过取消这样的监听器来修复它:

@Override
public void onDestroy() {
    super.onDestroy();
    if (myListener != null) {
        telephonyManager.listen(myListener, PhoneStateListener.LISTEN_NONE);
        myListener = null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM