Whatsapp通知过滤应用

时间:2017-06-09 19:34:04

标签: android notifications whatsapp accessibilityservice

我正在尝试构建Whatsapp通知过滤应用程序,我在其中监控来自Whatsapp的所有通知并使用某些过滤器删除消息。 所以有人可以帮助我如何捕获这些通知消息,以便我可以操纵它们 我知道我必须使用辅助功能服务,但我无法获得任何活动 我得到的只是null的事件源也是null

https://stackoverflow.com/users/2452075/ghmulchandani

我的代码是: - 使用辅助功能服务

MainActivity.java

package com.example.pavilion.accessebilityone;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    static TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView);
        Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivityForResult(intent, 0);

        Intent service = new Intent(this,WhatsappService.class);
        startService(service);
    }
}

辅助功能服务类: -

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pavilion on 6/6/2017.
 */
public class WhatsappService extends AccessibilityService {

    protected void onServiceConnected() {
        Toast.makeText(this, "connectd", Toast.LENGTH_SHORT).show();
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        info.notificationTimeout = 100;
        setServiceInfo(info);
    }

    public synchronized void onAccessibilityEvent(AccessibilityEvent event) {

        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {

            CharSequence pk = event.getPackageName();
            //Toast.makeText(this,cl,Toast.LENGTH_SHORT).show();
            Toast.makeText(this, pk, Toast.LENGTH_SHORT).show();
            Notification n = (Notification) event.getParcelableData();

            if (n == null)
                return;

            RemoteViews rv = n.contentView;
            View view = rv.apply(this, null);

            if (view == null)
                return;

            if (view instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) view;
                searchTextView(group);
            }
        }
    }

    @Override
    public void onInterrupt() {
    }


    private void searchTextView(ViewGroup group) {
        int count = group.getChildCount();

        for (int i = 0; i < count; i++) {
            View v = group.getChildAt(i);

            if (v instanceof TextView) {
               Toast.makeText(this,((TextView) v).getText(),Toast.LENGTH_SHORT).show();
            } else if (v instanceof ViewGroup) {
                searchTextView((ViewGroup) v);
            }
        }
    }
}

清单文件: -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pavilion.accessebilityone">
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".WhatsappService"

            android:enabled="true"
            android:exported="true"

            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.accessibilityservice.AccessibilityService"/>
            </intent-filter>
            <meta-data android:name="android.accessibilityservice"
                android:resource="@xml/accessibilityservice"/>


        </service>

    </application>

</manifest>

accessibility -service xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    android:accessibilityEventTypes="typeNotificationStateChanged"


    android:accessibilityFeedbackType="feedbackGeneric"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="100"

    android:settingsActivity="com.example.pavilion.accessebilityone.MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">



</accessibility-service>

1 个答案:

答案 0 :(得分:0)

您需要扩展通知侦听器服务。

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class WhatsappNotifcationListener extends NotificationListenerService {

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification statusBarNotification) {
        String packageName = statusBarNotification.getPackageName();
        Bundle extras = statusBarNotification.getNotification().extras;

        if (extras.getCharSequence("android.text") == null) {
            return;
        }

        String message = extras.getCharSequence("android.text").toString();
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        if (notificationTitle == null) {
            return;
        }

        handleNotification(packageName, notificationTitle, message);

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
        super.onNotificationRemoved(statusBarNotification);
    }

    private void handleNotification(String packageName, String notificationTitle, String message) {

        if(!packageName.equalsIgnoreCase("whatsapp packagename"))){
            return;
        }
        //Do something with notification title and notification message

    }

}

要提示用户提供通知访问权限,请在活动中使用以下内容:

activity.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

确保在标记中的清单文件中包含以下内容:

<service
    android:name=".WhatsappNotifcationListener"
    android:enabled="true"
    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>