AccessibilityService在重新启动时停止接收事件 [英] AccessibilityService stops receiving events on reboot

查看:251
本文介绍了AccessibilityService在重新启动时停止接收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用辅助功能服务来收听通知的应用程序.在用户重新启动之前,它可以正常工作.如果重新启动,则必须从辅助功能菜单中禁用/重新启用该服务.

I have an app that uses the accessibility services to listen to notifications. It works correctly unti the user reboots. If you reboot, you have to disable/re-enable the service from the accessibility services menu.

为什么重启后应用程序无法获取事件?

Why does the app not get the events after a reboot?

@Override
protected void onServiceConnected() {
    pMan = new PreferencesManager(this);
    bulbManager = new BulbManager(((Context) this), pMan.getBridge());
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.notificationTimeout = 100;
    info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
    setServiceInfo(info);
    Log.d("OMG, STARTED FINALLY!", "RIGHT NOW!");
}

和xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:packageNames="com.t3hh4xx0r.huenotifier"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="100"
    android:accessibilityFlags="flagDefault"
    android:settingsActivity="com.t3hh4xx0r.huenotifier.activities.LoadingSplashActivity"
    android:canRetrieveWindowContent="false"
    android:description="@string/app_name" />

和清单

   <service
        android:name="com.t3hh4xx0r.huenotifier.MyAccessibilityService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>

        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/service_info" />
    </service>

推荐答案

我遇到了同样的问题.经过大量尝试后,我发现此问题发生在旧版本的android(例如,豆形软糖)上.设备关闭电源后,服务将解除绑定.但是不会重新绑定 on_Boot_Completed .由于此问题未出现在较新版本的android上,因此我将其视为android bug.因为重新激活服务的唯一方法是关闭并重新启动服务,所以我使用 sharedPreference 来跟踪我的服务状态.当调用 onUnbind onRebind onServiceConnected 时,我用与之关联的值更新 sharedPreference .现在,在使用服务之前,我先检查服务是否调用了 onRebind onServiceConnected ,然后告诉用户重新启动服务或开始使用我的服务.

I was facing same problem. After a lot of attempt I found that this problem occurs with older version of android (eg.Jellybean). when device is powered off the service gets unbinded. But it is not rebinded on_Boot_Completed. As this problem doesn't appear on newer version of android I considered it as a android bug. Because only way to re-activate the service is turning off and restarting the service, I am using sharedPreference to keep track states of my service. when onUnbind onRebind or onServiceConnected is called I update my sharedPreference with the value I associated for it. Now, before using service I check that if the service called onRebind or onServiceConnected if not then I tell user to restart service or start working with my service.

如果您很困惑,则可以在可访问性服务中包含以下代码.

If you are so confused then you can Include the bellow code in your accessibility service.

public static String AccessibilityPreference="AccessibilityPreference";
public static String isServiceBinded="isServiceBinded";
@Override
public void onServiceConnected() {
    Log.e(TAG, "ACC::onServiceConnected:************************* ");
    SharedPreferences p=getSharedPreferences(AccessibilityPreference,Context.MODE_PRIVATE);
    SharedPreferences.Editor e=p.edit();
    e.putBoolean(isServiceBinded,true);
    e.commit();
}
 @Override
public boolean onUnbind(Intent intent) {
    Log.e("onUnbind","onUnbind Called***************************************************");
    SharedPreferences p=getSharedPreferences(AccessibilityPreference,Context.MODE_PRIVATE);
    SharedPreferences.Editor e=p.edit();
    e.putBoolean(isServiceBinded,false);
    e.commit();
    return super.onUnbind(intent);
}

@Override
public void onRebind(Intent intent) {
    Log.e("onRebind","onRebind Called***************************************************");
    SharedPreferences p=getSharedPreferences(AccessibilityPreference,Context.MODE_PRIVATE);
    SharedPreferences.Editor e=p.edit();
    e.putBoolean(isServiceBinded,true);
    e.commit();
    super.onRebind(intent);
}
public static boolean isServiceBinded(SharedPreferences preferences){
   return preferences.getBoolean(isServiceBinded,true);
}

然后在开始使用服务之前,先检查服务是否如波纹管一样有效,

Then before start working with the service check if service is valid like bellow,

if(AccessibilityWorkService.isServiceBinded(getSharedPreferences(AccessibilityWorkService.AccessibilityPreference, Context.MODE_PRIVATE))){
//start working
}else {
//tell user to restart service
}

这篇关于AccessibilityService在重新启动时停止接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆