11

我写了一个非常简单的accessibilityService,我试图让服务的描述出现在Settings/accessibility/my app中。下面是我的所有代码,应该将描述设置为“你好!” 但设置仍然显示“没有可用的描述”

在此处输入图像描述

我的服务:

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/ 我有 strings.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="accessibility_service_description">Hello!</string>
</resources>
4

1 回答 1

8

我遇到了同样的问题并最终让它工作。我知道服务本身包含权限,但您还需要将BIND_ACCESSIBILITY_SERVICE权限添加到清单中。

<manifest ...>
    ...
    <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
    ...
</manifest>

服务的<meta-data>名称必须命名为android.accessibilityservice您拥有的名称,这样很好。

最后,我不得不将 apk 推送到我的设备,然后重新启动它以进行更改。

注意:在清单的元素中添加description属性不会影响系统设置中显示的服务描述。<service>

于 2015-07-09T12:25:03.617 回答