在模拟器上运行时,并不总是调用AccessibilityService onUnbind

时间:2015-02-26 20:34:44

标签: android accessibilityservice

我正在为Android开发辅助功能服务。问题是,当在模拟器(Android 4.0.5 API 15)上运行时,在关闭服务时不会调用有时 onUnbind。发生这种情况时,无法停止服务,我需要重新安装apk或重新启动模拟器。

在实际设备(Galaxy Nexus)上运行此辅助功能服务时,它可以正常工作。我无法重现这个问题。

相关服务代码如下:

public class MyService extends AccessibilityService {
    boolean mRunning= false;

    private void init() {
        if (mRunning) {
            // if execution reaches this point means that onUnbind was
            // not called the last time the service has been switched off                    
            return;
        }

        /**
         * Unsubscribe all accessibility events. Cannot be removed directly from
         * @xml/accessibilityservice, otherwise onUnbind and onDestroy // never
         * get called
         */
        setServiceInfo(new AccessibilityServiceInfo());

        // Initialization stuff

        mRunning= true;
    }

    private void cleanup() {
        if (!mRunning) return;

        // cleanup stuff

        mRunning= false;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onServiceConnected() {

        init();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        cleanup();

        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        cleanup();
    }
}

问题:

  • 我错过了什么吗?
  • 我能够检测到这种情况何时发生(请参阅有关init()的注释)。有没有办法强迫服务停止?

其他详细信息:

  • 该服务包括本机部分并生成两个线程
  • 在模拟器上消耗100%的CPU,在Galaxy Nexus上占35%
  • 第一次失败后,无论打开和关闭多少次服务,onUnbind都不会再被调用。

0 个答案:

没有答案
相关问题