Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

java - Read Notification Bar title, message using Accessibility Service Programmatically

Using Accessibility Service I am being able to read notification bar title & message, the issue I am facing is when first notification appear I am reading all these perfectly but after first notification & onward I am only getting title & text "you have 2 messages" and so on, not the entire message. Waiting for your expert advice.

Code :

@Override
    protected void onServiceConnected() 
    {
        Log.d("AccessibilityServiceNotification", "ServiceConnected");
    try
    {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();

        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;

        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;

        info.notificationTimeout = 100;

        setServiceInfo(info);
    }
    catch(Exception e)
    {
        Log.d("ERROR onServiceConnected", e.toString());
    }
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) 
    {
        try 
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            Parcelable data = event.getParcelableData();

            if(data !=null)
            {
                Notification notification = (Notification) data;

                RemoteViews remoteView = notification.bigContentView;

                ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null);

                remoteView.reapply(getApplicationContext(), localView);

                Resources resources = null;

                PackageManager pkm = getPackageManager();

                try 
                {
                    resources = pkm.getResourcesForApplication("com.user.package");
                }
                catch (NameNotFoundException e) 
                {
                    e.printStackTrace();
                }

                if (resources == null)
                    return; 

                int TITLE = resources.getIdentifier("android:id/title", null, null);

                int INBOX = resources.getIdentifier("android:id/big_text", null, null);

                int TEXT = resources.getIdentifier("android:id/text", null, null);

                String packagename = String.valueOf(event.getPackageName());

                title = (TextView) localView.findViewById(TITLE);

                inbox = (TextView) localView.findViewById(INBOX);

                text = (TextView) localView.findViewById(TEXT);

                Log.d("NOTIFICATION Package : ", packagename);

                Log.d("NOTIFICATION Title : ", title.getText().toString());

                Log.d("NOTIFICATION You have got x messages : ", text.getText().toString());

                Log.d("NOTIFICATION inbox : ", inbox.getText().toString());
        }
        }
        catch(Exception e)
        {
            Log.e("onAccessibilityEvent ERROR", e.toString());
        } 
    }

Example Notification 1:

package : com.whatsapp, title : Hello, message: How are you

Example Notification 2:

package : com.whatsapp, title : Hello, message: you have 2 messages (instead of : What are you doing)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is rather simple by using the extras field in the notification. The key that holds the expanded text lines is EXTRA_TEXT_LINES.

This is working for me:

Notification notification = (Notification) event.getParcelableData();
CharSequence[] lines = notification.extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
int i = 0;
for(CharSequence msg : lines)
{
    Log.d("Line " + i, (String) msg);
    i += 1;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...