以编程方式使用辅助功能服务读取通知栏标题,消息

时间:2015-03-11 06:35:46

标签: java android notifications accessibilityservice

使用辅助功能服务我可以阅读通知栏标题&消息,我面临的问题是当第一次通知出现时我正在完全阅读所有这些但是在第一次通知之后&我只获得头衔和奖励文字"你有2条消息"等等,而不是整个消息。 等待你的专家意见。

代码:

@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());
        } 
    }

示例通知1:

包:com.whatsapp, 标题:你好, 消息:你好吗

示例通知2:

包:com.whatsapp, 标题:你好, 消息:你有2条消息(而不是:你在做什么)

4 个答案:

答案 0 :(得分:6)

通过使用通知中的extras字段非常简单。保存展开文本行的键是EXTRA_TEXT_LINES

这对我有用:

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;
}

答案 1 :(得分:2)

这段代码非常适合我:

List<String> msgs = new ArrayList<String>();
msgs.add("Info: " + notif.extras.getString(Notification.EXTRA_INFO_TEXT));
msgs.add("Title: " + notif.extras.getString(Notification.EXTRA_TITLE));
msgs.add("Text: " + notif.extras.getString(Notification.EXTRA_TEXT));

我研究了几天来获得这种方法,我已经在Android 4.4,5.5和6.0上进行了测试。您也可以使用方法

if(event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED){  
  if(event.getPackageName().toString().equalsIgnoreCase("com.whatsapp")){
Log.d("msg", event.getText().toString().replace("[", "]").replaceAll("]", ""), Logger.EXTRA_LOG);
 }
}

然后你就可以得到所有书面信息。 希望它有所帮助。

答案 2 :(得分:0)

试试这个,下面的代码适合我 -

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();
}
希望它会对你有所帮助。

<强> EDITED

在res文件夹中创建一个名为xml的文件夹 - 在其中创建一个名为 - &#34; accessibilityservice&#34;的xml。并粘贴在代码下面 -

<?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" />

和内部清单将您的服务标签更新为以下代码 -

<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> 

答案 3 :(得分:0)

in 2 days