5

アクセシビリティサービスを使用して、通知バーのタイトルとメッセージを読むことができます。直面している問題は、最初の通知が表示されたときにこれらすべてを完全に読んでいますが、最初の通知以降はタイトルとテキスト「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

4 に答える 4

9

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;
}
于 2015-07-04T02:27:41.767 に答える
5

このコードは私にとって完璧に機能します:

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

その後、すべての書かれたメッセージを取得できます。それが役に立てば幸い。

于 2016-08-27T18:11:24.830 に答える