gpt4 book ai didi

java.lang.IllegalStateException : android.accessibilityservice.AccessibilityService.getSystemService(AccessibilityService.java:1602)

转载 作者:行者123 更新时间:2023-12-01 16:16:28 25 4
gpt4 key购买 nike

我在应用程序中使用辅助功能服务向用户读出自定义文本。这是我的服务类别:

class GlobalAccessibilityService: AccessibilityService() {
override fun onInterrupt() {
Log.i("Accessibility","onInterrupt()")
}

override fun onAccessibilityEvent(event: AccessibilityEvent?) {
Log.i("Accessibility","onAccessibilityEvent() & Even is: "+event.toString())
}
}

我已经在 Manifest 的 Service 标签下声明了它,并且也有相应的 xml 文件。

这就是我在 Activity 类中调用服务的方式:




var mService = GlobalAccessibilityService()

var manager: AccessibilityManager = mService.getSystemService(Context.ACCESSIBILITY_SERVICE)

if (manager.isEnabled){
var event: AccessibilityEvent = AccessibilityEvent.obtain()
event.eventType = AccessibilityEvent.TYPE_ANNOUNCEMENT
event.text.add("This is a test for Accessibility Service Announcement")
manager.sendAccessibilityEvent(event)
}

但是,我收到以下错误:


java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.accessibilityservice.AccessibilityService.getSystemService(AccessibilityService.java:1602)

请求帮助!谢谢。

最佳答案

看起来您正在调用下面编写的 onCreate() 方法之前的内容。您在哪里调用以下代码? :

var mService = GlobalAccessibilityService()

var manager: AccessibilityManager = mService.getSystemService(Context.ACCESSIBILITY_SERVICE)

if (manager.isEnabled){
var event: AccessibilityEvent = AccessibilityEvent.obtain()
event.eventType = AccessibilityEvent.TYPE_ANNOUNCEMENT
event.text.add("This is a test for Accessibility Service Announcement")
manager.sendAccessibilityEvent(event)
}

关于java.lang.IllegalStateException : android.accessibilityservice.AccessibilityService.getSystemService(AccessibilityService.java:1602),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62378583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com