Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式使用辅助功能服务读取通知栏标题、消息_Java_Android_Notifications_Accessibilityservice - Fatal编程技术网

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

Java 以编程方式使用辅助功能服务读取通知栏标题、消息,java,android,notifications,accessibilityservice,Java,Android,Notifications,Accessibilityservice,使用可访问性服务,我能够阅读通知栏标题和消息,我面临的问题是,当第一次通知出现时,我正在完美地阅读所有这些内容,但在第一次通知及以后,我只收到标题和文本“Youhave2messages”等等,而不是整条消息。 等待您的专家建议 代码: @Override protected void onServiceConnected() { Log.d("AccessibilityServiceNotification", "ServiceConnected");

使用可访问性服务,我能够阅读通知栏标题和消息,我面临的问题是,当第一次通知出现时,我正在完美地阅读所有这些内容,但在第一次通知及以后,我只收到标题和文本“Youhave2messages”等等,而不是整条消息。 等待您的专家建议

代码:

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

@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());
        } 
    }
包装:com.whatsapp, 标题:你好, 留言:你好吗

通知示例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());
        } 
    }
包装:com.whatsapp, 标题:你好,
消息:您有两条消息(而不是:您在做什么)

试试这个,下面的代码对我有用-

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();
}
Notification Notification=(Notification)event.getParcelableData();
RemoteViews=notification.contentView;
Class secretClass=views.getClass();
试一试{
映射文本=新的HashMap();
Field outerFields[]=secretClass.getDeclaredFields();
for(int i=0;i
希望它能帮助你

已编辑

在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="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> 

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

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

使用通知中的
附加项
字段相当简单。保存展开文本行的键是
额外文本行

这对我有用:

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

此代码对我来说非常适用:

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));
然后你可以得到所有的书面信息。 希望能有所帮助。


            <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="deepak.com.androidnotification">
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name="deepak.com.androidnotification.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service
                android:name="deepak.com.androidnotification.NotificationService"
                android:label="@string/app_name"
                android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

                <intent-filter>

                    <action android:name="android.service.notification.NotificationListenerService" />

                </intent-filter>

            </service>
        </application>

    </manifest>




    import android.app.Notification;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.service.notification.NotificationListenerService;
    import android.service.notification.StatusBarNotification;
    import android.util.Log;
    import android.support.v4.content.LocalBroadcastManager;

    import java.io.ByteArrayOutputStream;


    public class NotificationService extends NotificationListenerService {

        Context context;

        @Override

        public void onCreate() {

            super.onCreate();
            context = getApplicationContext();

        }
        @Override

        public void onNotificationPosted(StatusBarNotification sbn) {
            String pack = sbn.getPackageName();
            String ticker ="";
            if(sbn.getNotification().tickerText !=null) {
                ticker = sbn.getNotification().tickerText.toString();
            }
            Bundle extras = sbn.getNotification().extras;
            String title = extras.getString("android.title");
           String text = extras.getCharSequence("android.text").toString();
            int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);


            Log.i("Package",pack);
            Log.i("Ticker",ticker);
            Log.i("Title",title);
           Log.i("Text",text);

            Intent msgrcv = new Intent("Msg");
            msgrcv.putExtra("package", pack);
            msgrcv.putExtra("ticker", ticker);
            msgrcv.putExtra("title", title);
           msgrcv.putExtra("text", text);

            LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


        }

        @Override

        public void onNotificationRemoved(StatusBarNotification sbn) {
            Log.i("Msg","Notification Removed");

        }
    }




    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    import java.util.ArrayList;

    /**
     * Created by Deepak on 8/30/2017.
     */

    public class CustomListAdapter extends BaseAdapter {
        int mResource;
        ArrayList<Data> arrayList;
        Context context;

        public CustomListAdapter(Context context,int resource, ArrayList<Data> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
            this.mResource=resource;
        }
        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Data data = (Data) getItem(position);
            LayoutInflater inflater =LayoutInflater.from(context);
            convertView=inflater.inflate(mResource,parent,false);
            TextView textView=(TextView)convertView.findViewById(R.id.textView);
            TextView textView1=(TextView)convertView.findViewById(R.id.textView2);
            TextView textView2=(TextView)convertView.findViewById(R.id.textView3);
            textView.setText(data.getName());
            textView1.setText(data.getPack());
            textView2.setText(data.getText());
            return convertView;
        }
    }




    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v4.content.LocalBroadcastManager;
    import android.util.Log;
    import android.widget.ListView;
    import java.util.ArrayList;

    public class MainActivity extends Activity {
       ListView listView;
        ArrayList<Data> list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView=(ListView)findViewById(R.id.list) ;
            LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
        }

        private BroadcastReceiver onNotice= new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String title = intent.getStringExtra("title");
                String pack= intent.getStringExtra("package");
                String text = intent.getStringExtra("text");
                Log.d("Title",title);
                Data data=new Data();
                data.setName(title);
                data.setPack(pack);
                data.setText(text);
                list=new ArrayList<Data>();
                list.add(data);
                listView.setAdapter(new CustomListAdapter(MainActivity.this,R.layout.list_view,list));
            }
        };
    }




public class Data {
    private String name;
    private String pack;
    private String text;

    public Data() {
    }

    public Data(String name, String pack, String text) {
        this.name = name;
        this.pack = pack;
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPack() {
        return pack;
    }

    public void setPack(String pack) {
        this.pack = pack;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:gravity="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:gravity="center"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="24dp"
        android:gravity="center"
        android:text="TextView"/>

</RelativeLayout>




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ListView
       android:layout_width="match_parent"
       android:id="@+id/list"
       android:layout_height="wrap_content"/>



</RelativeLayout>
导入android.app.Notification; 导入android.content.Context; 导入android.content.Intent; 导入android.graphics.Bitmap; 导入android.os.Bundle; 导入android.service.notification.NotificationListenerService; 导入android.service.notification.StatusBarNotification; 导入android.util.Log; 导入android.support.v4.content.LocalBroadcastManager; 导入java.io.ByteArrayOutputStream; 公共类NotificationService扩展了NotificationListenerService{ 语境; @凌驾 public void onCreate(){ super.onCreate(); context=getApplicationContext(); } @凌驾 公告发布时的公共作废(状态公告sbn){ String pack=sbn.getPackageName(); String-ticker=“”; if(sbn.getNotification().tickerText!=null){ ticker=sbn.getNotification().tickerText.toString(); } Bundle extras=sbn.getNotification().extras; String title=extras.getString(“android.title”); String text=extras.getCharSequence(“android.text”).toString(); intID1=extras.getInt(Notification.EXTRA\u SMALL\u图标); Log.i(“包装”,包装); Log.i(“股票代码”,股票代码); 日志i(“标题”,标题); Log.i(“文本”,文本); 意向msgrcv=新意向(“Msg”); msgrcv.putExtra(“包装”,包装); msgrcv.putExtra(“股票代码”,股票代码); msgrcv.putExtra(“标题”,标题); msgrcv.putExtra(“文本”,文本); LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv); } @凌驾 已删除通知上的公共无效(状态通知sbn){ Log.i(“Msg”,“删除通知”); } } 导入android.content.Context; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.BaseAdapter; 导入android.widget.TextView; 导入java.util.ArrayList; /** *由Deepak于2017年8月30日创建。 */ 公共类CustomListAdapter扩展了BaseAdapter{ 国际资源; ArrayList ArrayList; 语境; 公共CustomListAdapter(上下文上下文、int资源、ArrayList ArrayList){ this.context=上下文; this.arrayList=arrayList; this.mResource=资源; } @凌驾 public int getCount(){ 返回arrayList.size(); } @凌驾 公共对象getItem(int位置){ 返回arrayList.get(位置); } @凌驾 公共长getItemId(int位置){ 返回位置;