Android Accessibility Service Detect Notification

How to read Notifications using Accessibility Services

https://developer.android.com/reference/android/view/accessibility/AccessibilityRecord#getParcelableData()

Different types of event carry different data; For this particular kind you use getParcelableData() to get the notification body.

case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
Notification notification = (Notification) accessibilityEvent.getParcelableData();
if (notification == null) return;
String title = notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString();
String text = notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
String text2 = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT).toString();
String package_name = accessibilityEvent.getPackageName().toString();
break;

Android accessibility service detect notification

Accessibility services in Android 4.0 and above can behave strangely if there is no accessibility-service meta-data tag defined in the manifest. Try defining the meta-data as in the examples below. You should continue to use setServiceInfo() to maintain backward compatibility with pre-4.0 devices.

Also, I would recommend specifying a feedback type that is specific to your service, rather than using "all".

AndroidManifest.xml

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

res/xml/accessibilityservice.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100" />

There was an error in your feedbackType. Corrected below. Still, consider using a more specific feedback type.

NotificationService.java

@Override
protected void onServiceConnected() {
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.notificationTimeout = 100;
setServiceInfo(info);
}

Android accessibility service

I added a meta data element inside the service tag

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

And in the xml resources file I have

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100" />

Cannot access notification through Android AccessibilityService

I got it working on L though, it's not really working on api 16, I tested on one. Can anyone suggest an addition to make it work on api 14+

Manifest File

package com.example.tony.acctest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.tony.acctest.MyAccessibilityService.Constants;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MA LOG";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentFilter mIntentFilter = new IntentFilter(Constants.ACTION_CATCH_NOTIFICATION);
registerReceiver(NotificationCatcherReceiver, mIntentFilter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(NotificationCatcherReceiver);
}

private final BroadcastReceiver NotificationCatcherReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, intent.getStringExtra(Constants.EXTRA_PACKAGE));
Log.v(TAG, intent.getStringExtra(Constants.EXTRA_MESSAGE));
}
};
}

My service class

package com.example.tony.acctest;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Parcelable;
import android.view.accessibility.AccessibilityEvent;

import java.util.List;

public class MyAccessibilityService extends AccessibilityService {

private final AccessibilityServiceInfo info = new AccessibilityServiceInfo();

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

final int eventType = event.getEventType();
if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
final String sourcePackageName = (String)event.getPackageName();
Parcelable parcelable = event.getParcelableData();

if (parcelable instanceof Notification) {
List<CharSequence> messages = event.getText();
if (messages.size() > 0) {
try {
final String notificationMsg = (String) messages.get(0);
Intent mIntent = new Intent(Constants.ACTION_CATCH_NOTIFICATION);
mIntent.putExtra(Constants.EXTRA_PACKAGE, sourcePackageName);
mIntent.putExtra(Constants.EXTRA_MESSAGE, notificationMsg);
MyAccessibilityService.this.getApplicationContext().sendBroadcast(mIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

@Override
public void onInterrupt() {

}

@Override
public void onServiceConnected() {
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
} else {
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
}
info.notificationTimeout = 100;
this.setServiceInfo(info);
}

public static final class Constants {

public static final String EXTRA_MESSAGE = "extra_message";
public static final String EXTRA_PACKAGE = "extra_package";
public static final String ACTION_CATCH_NOTIFICATION = "com.example.tony.acctest.CATCH_NOTIFICATION";
}

}

This is my Main activity
package com.example.tony.acctest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.tony.acctest.MyAccessibilityService.Constants;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MA LOG";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentFilter mIntentFilter = new IntentFilter(Constants.ACTION_CATCH_NOTIFICATION);
registerReceiver(NotificationCatcherReceiver, mIntentFilter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(NotificationCatcherReceiver);
}

private final BroadcastReceiver NotificationCatcherReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, intent.getStringExtra(Constants.EXTRA_PACKAGE));
Log.v(TAG, intent.getStringExtra(Constants.EXTRA_MESSAGE));
}
};
}

Accessibility service notification removed

You should use NotificationListenerService for this purpose (To catch Notification of application NotificationListenerService is another option like Accessibility Service).

Where onNotificationRemoved gives StatusBarNotification into parameter. By reading this param you can get all information about notification.

You can read NotificationListenerService-Example for more details.

Read Notification Bar title, message using Accessibility Service Programmatically

try this, below code works for me -

Notification notification = (Notification) event.getParcelableData();
RemoteViews views = notification.contentView;
Class secretClass = views.getClass();

try {
Map<Integer, String> text = new HashMap<Integer, String>();

Field outerFields[] = secretClass.getDeclaredFields();
for (int i = 0; i < outerFields.length; i++) {
if (!outerFields[i].getName().equals("mActions")) continue;

outerFields[i].setAccessible(true);

ArrayList<Object> actions = (ArrayList<Object>) outerFields[i]
.get(views);
for (Object action : actions) {
Field innerFields[] = action.getClass().getDeclaredFields();

Object value = null;
Integer type = null;
Integer viewId = null;
for (Field field : innerFields) {
field.setAccessible(true);
if (field.getName().equals("value")) {
value = field.get(action);
} else if (field.getName().equals("type")) {
type = field.getInt(action);
} else if (field.getName().equals("viewId")) {
viewId = field.getInt(action);
}
}

if (type == 9 || type == 10) {
text.put(viewId, value.toString());
}
}

System.out.println("title is: " + text.get(16908310));
System.out.println("info is: " + text.get(16909082));
System.out.println("text is: " + text.get(16908358));
}
} catch (Exception e) {
e.printStackTrace();
}

hope it will help you.

EDITED

create a folder named xml inside your res folder - create a xml in it named -"accessibilityservice" and paste below code -

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100" />

and inside manifest update your service tag to below code -

<service
android:name=".YourServiceClassName"
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/accessibilityservice" />
</service>

Android: How do you check if a particular AccessibilityService is enabled

I worked this one out myself in the end:

public boolean isAccessibilityEnabled() {
int accessibilityEnabled = 0;
final String LIGHTFLOW_ACCESSIBILITY_SERVICE = "com.example.test/com.example.text.ccessibilityService";
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (SettingNotFoundException e) {
Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}

TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

if (accessibilityEnabled==1) {
Log.d(LOGTAG, "***ACCESSIBILIY IS ENABLED***: ");

String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Log.d(LOGTAG, "Setting: " + settingValue);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
Log.d(LOGTAG, "Setting: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)){
Log.d(LOGTAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}

Log.d(LOGTAG, "***END***");
}
else {
Log.d(LOGTAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}


Related Topics



Leave a reply



Submit