2

How to get the notifications in android using Accessibility Service above 4.0?

Why onAccessibilityEvent(AccessibilityEvent event) not calling When Notification fired above 4.0 Please give Proper Suggestion

Thanks in advance

@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        final int eventType = event.getEventType();
        System.out.println("MyServ.onAccessibilityEvent()");
        switch (eventType) {
        case AccessibilityEvent.TYPE_VIEW_FOCUSED:
            Toast.makeText(getApplicationContext(), "Notification fired",
                    Toast.LENGTH_LONG).show();
            break;
        case AccessibilityEvent.TYPE_ANNOUNCEMENT:
            Toast.makeText(getApplicationContext(), "Type is Announcement",
                    Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }

    }

In onServiceConnected()

@Override
    protected void onServiceConnected() {
        super.onServiceConnected();
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        setServiceInfo(info);
}
hareesh J
  • 520
  • 1
  • 6
  • 21

1 Answers1

1

Try this code :

    private void generateNotification(Context context, String message,
        long when, String query) {

    int icon = R.drawable.icon;
         long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
            notification = new Notification(icon, message, when);
            notification.setLatestEventInfo(context, appname, message,
                    contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify((int) when, notification);

        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(appname).setWhen(when)
                    .setAutoCancel(true).setContentTitle(appname)
                    .setContentText(message).build();

            notificationManager.notify((int) when, notification);

        }

}

You can call generateNotification() method in onAccessibilityEvent() and notifications will be displayed.

Robert Karl
  • 7,598
  • 6
  • 38
  • 61
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
  • your answer working fine but when i got any message or call,In the Notification status bar multiple notifications showing.It should display single notification per single event @androiduser – hareesh J Oct 25 '13 at 05:36
  • For single notification you should use : notificationManager.notify(0, notification); instead of notificationManager.notify((int) when, notification); – Siddharth_Vyas Oct 25 '13 at 06:07