Android 无障碍服务未启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11087758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessibility service is not started
提问by POMATu
I want to log all toasts events in android ics (4.0.3), but I was unable to log any system event. Service is just not started!
我想在 android ics (4.0.3) 中记录所有 toasts 事件,但我无法记录任何系统事件。服务还没有开始!
According to this question: onAccessibilityEvent(AccessibilityEvent event) not intercepting notification
根据这个问题: onAccessibilityEvent(AccessibilityEvent event) 不拦截通知
MyAccessibilityService.java
我的无障碍服务.java
package com.test.toasts2;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.os.Parcelable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;
public class MyAccessibilityService extends AccessibilityService {
public static final String TAG = "volumeMaster";
@Override
public void onAccessibilityEvent(AccessibilityEvent event)
{
Log.v(TAG, "***** onAccessibilityEvent");
Toast.makeText(getApplicationContext(), "Got event from: " + event.getPackageName(), Toast.LENGTH_LONG).show();
}
@Override
public void onInterrupt()
{
Log.v(TAG, "***** onInterrupt");
}
@Override
public void onServiceConnected()
{
Log.v(TAG, "***** onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.notificationTimeout = 100;
info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
setServiceInfo(info);
}
}
Toast2Activity.java
Toast2Activity.java
package com.test.toasts2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class Toast2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, MyAccessibilityService.class);
startService(i);
}
}
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.toasts2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".MyAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
<activity
android:name=".Toast2Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I included activity tag in manifest (to start service on app start), and also tried to remove. Nothing changed. Service is just not started. I don't get notification in log cat (Log.v in onServiceConected).
我在清单中包含了活动标签(在应用程序启动时启动服务),并尝试删除。没有改变。服务只是没有启动。我在日志猫(onServiceConected 中的 Log.v)中没有收到通知。
I am compiling this as normal app (not system app), android 4.0.3. Am I doing something wrong?
我将它编译为普通应用程序(不是系统应用程序),android 4.0.3。难道我做错了什么?
Attached project (may be mistake is somewhere else, or maybe I am compiling it wrong): https://dl.dropbox.com/u/1928109/toast2.zip
附加项目(可能是其他地方的错误,或者我编译错了):https: //dl.dropbox.com/u/1928109/toast2.zip
回答by Gunnar Karlsson
You need to enable the accessibility service from the system settings menu:
您需要从系统设置菜单启用无障碍服务:
Install your app on the testing device.
With the app installed on the device, change the following setting on the device:
Home Screen > System Settings > Accessibility > Accessibility Services > Toast2 > Change from Off to On,
where Toast2 is your app name.
Then run your app.
在测试设备上安装您的应用。
在设备上安装应用程序后,更改设备上的以下设置:
主屏幕 > 系统设置 > 辅助功能 > 辅助功能服务 > Toast2 > 从关闭更改为开启,
其中 Toast2 是您的应用程序名称。
然后运行您的应用程序。
AFAIK there is no method to set accessibility programmatically to On mode from within your app. As a workaround you can prompt the user to change the setting and if they agree by clicking a button, launch the accessibility system settings like so:
AFAIK 没有方法可以在您的应用程序中以编程方式将辅助功能设置为开启模式。作为一种解决方法,您可以提示用户更改设置,如果他们同意单击按钮,则启动辅助功能系统设置,如下所示:
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
回答by candy
As a complementary answer to the comments, one more thing worth noting is that you have to declare permission - android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"in your service, or else there will be no switch in setting->accessibility.
作为对评论的补充回答,还有一点值得注意的是,您必须在您的服务中声明权限 - android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE",否则设置-> 辅助功能将无法切换。