android service 里面怎么调用返回键


RT ,我就是想在service 里面 调用一下返回键。 现在网上大多数都是 在 Activity里面调用的返回键。 请问在service里面可以实现吗。

service Android

china 10 years, 7 months ago

借助 AccessibilityService 辅助服务的AccessibilityEvent.getSource().performAction(AccessibilityService.GLOBAL_ACTION_BACK);
可以实现,不过还需要用户去开启服务,可行性不高,
或者你试试异步发送 new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);不知道这个在services里面有没有效果。

slovy answered 10 years, 7 months ago

Activity 可以响应 KeyEvent ,所以你调用 dispatchKeyEvent 的时候,是 Activity 或者 DecorView 进行响应你的 KeyEvent ,但是 Service 并没有 Window 和它进行绑定,所以你不能在当前上下文中调用返回键。

如果你想在系统级别调用返回键,是需要系统级的签名的(也就是改Android OS的源码,重新编译)

梓喵的木炭 answered 10 years, 7 months ago

Your Answer