我要记录在Android的集成电路(4.0.3)所有敬酒事件,但我无法登录任何系统事件。服务是不启动!
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!
根据这个问题: onAccessibilityEvent(AccessibilityEvent事件)不拦截通知
MyAccessibilityService.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
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中
<?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>
我列入活动代码清单中(开始对应用程序启动服务),并且还试图删除。没有改变。只是不启动服务。我没有得到通知日志猫(Log.v在onServiceConected)。
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).
我编这是正常的应用程序(而不是系统的应用程序),安卓4.0.3。难道我做错了什么?
I am compiling this as normal app (not system app), android 4.0.3. Am I doing something wrong?
连接项目(可能是错误的是别的地方,也许我编译错了): https://dl.dropbox.com/u/1928109/toast2.zip
Attached project (may be mistake is somewhere else, or maybe I am compiling it wrong): https://dl.dropbox.com/u/1928109/toast2.zip
推荐答案
您需要启用从系统设置菜单中的可访问性服务:
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.
AFAIK没有办法从你的应用程序中以编程方式在模式设置可访问性。作为一种变通方法,你可以提示用户更改设置,如果他们同意通过点击一个按钮,启动辅助系统设置,像这样:
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:
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
相关推荐
最新文章