我写了一个非常简单的accessibilityService,我想在设置/辅助功能/我的应用中显示该服务的描述。下面是我的所有代码,应将描述设置为“Hello!”但设置仍显示“无可用说明”
我的服务:
public class IdleService extends AccessibilityService{
int interval = 100;
int maxIdleTime = 20000;
int idleTime = 0;
Handler mHandler;
String cProcess;
@Override
public void onServiceConnected(){
mHandler = new Handler();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
// we are interested in all types of accessibility events
info.describeContents();
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
// we want to receive events in a certain interval
info.notificationTimeout = interval;
setServiceInfo(info);
Log.e("IdleService", " idle service connected!");
statusChecker.run();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent arg0)
{
// TODO Auto-generated method stub
Log.v("IdleService", "reseting idle time");
idleTime = 0;
}
@Override
public void onInterrupt()
{
// TODO Auto-generated method stub
Log.v("IdleService", "INTERUPTED");
}
public void setMaxIdleTimeInSeconds(int time)
{
maxIdleTime = time *1000;
}
Runnable statusChecker = new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
ActivityManager am = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> processList = am
.getRunningTasks(5);
cProcess = processList.get(0).baseActivity.getPackageName();
Log.v("IdleService", "Current PackageName:"+cProcess);
cProcess = processList.get(0).baseActivity.getClassName();
Log.v("IdleService", "Current ClassName:"+cProcess);
idleTime +=100;
Log.v("IdleService", "TICK TOCK, user Idle for:"+idleTime);
if(idleTime>=maxIdleTime)
{
Log.v("IdleService", "we timed out do to inactivity");
//mHandler.postDelayed(statusChecker, interval);
//stopSelf();
}else{
}
mHandler.postDelayed(statusChecker, interval);
}
};
}
在我的清单中我有这个服务:
<service android:name="com.myPackage.IdleService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
在我的res / xml中我有一个accessibility_service_config.xml文件:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>
最后在我的res / values / i中有strings.xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="accessibility_service_description">Hello!</string>
</resources>
答案 0 :(得分:7)
我遇到了同样的问题并最终让它发挥作用。我知道服务本身包含权限,但您还需要向清单添加BIND_ACCESSIBILITY_SERVICE
权限。
<manifest ...>
...
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
...
</manifest>
服务的<meta-data>
必须命名为android.accessibilityservice
,这样才能做到这一点。
最后,我不得不将apk推送到我的设备,然后重新启动它以进行更改。
注意:在清单的description
元素中添加<service>
属性不会影响系统设置中显示的服务说明。< / p>