简体   繁体   中英

Android Accessibility Service not receiving Notification in oreo

I have added android accessibility in my app and it is working fine with pre oreo devices , but in oreo i am not able to receive second onwards notifications for the app .

I can only receive first notification of the app, second notification not able to receive for that app. If i will clear notification from the notification tray then i am able to receive first notification and second onwards not received .

for eg

  1. notification1 received from whatsapp , service can read it
  2. notification2 received from whatsapp , service can't read it
  3. notification3 received from Gmail , service can read it
  4. notification4 received from Gmail , service can't read it
  5. notification5 received from whatsapp , service can't read it

in short , second onwards notification accessibility ignores in oreo from app .

My Accessibility code ,

    public class NotificationService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent evt) {
     Toast.makeText(this, "Got event from " + evt.getPackageName(), 
    Toast.LENGTH_SHORT)
            .show();
      }

      @Override
  public void onInterrupt() { }
}

My manifest file :

<application>
    <activity
        android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".NotificationService" android:enabled="true" 
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>
</application>

The Accessibility service might stop without notifying the user, it's a known issue. Kindly refer the below code to check whether the Service is still running or not.

  // To check if service is enabled
        public boolean isAccessibilitySettingsOn(Context mContext) {
            int accessibilityEnabled = 0;
            final String service = getPackageName() + "/" + CustomAccessiblityService.class.getCanonicalName();
            try {
                accessibilityEnabled = Settings.Secure.getInt(
                        mContext.getApplicationContext().getContentResolver(),
                        android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
                Log.v(getString(R.string.app_name), "accessibilityEnabled = " + accessibilityEnabled);

                //accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);

            } catch (Settings.SettingNotFoundException e) {
                Log.e(getString(R.string.app_name), "Error finding setting, default accessibility to not found: "
                        + e.getMessage());
            }
            TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

            if (accessibilityEnabled == 1) {
                Log.v(getString(R.string.app_name), "***ACCESSIBILITY IS ENABLED*** -----------------");
                String settingValue = Settings.Secure.getString(
                        mContext.getApplicationContext().getContentResolver(),
                        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
                if (settingValue != null) {
                    mStringColonSplitter.setString(settingValue);
                    while (mStringColonSplitter.hasNext()) {
                        String accessibilityService = mStringColonSplitter.next();

                        Log.v(getString(R.string.app_name), "-------------- > accessibilityService :: " + accessibilityService + " " + service);
                        if (accessibilityService.equalsIgnoreCase(service)) {
                            Log.v(getString(R.string.app_name), "We've found the correct setting - accessibility is switched on!");
                            return true;
                        }
                        else{
                            startActivityForResult(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0);
                        }
                    }
                }
            } else {
                Log.v(getString(R.string.app_name), "***ACCESSIBILITY IS DISABLED***");
                startActivityForResult(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0);
            }

            return false;
        }

Try to use this method in your service so that you won't miss the notification again and again. Hopefully it will help. Kindly accept the answer if it helps. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM