我正在为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();
}
}
问题:
其他详细信息: