计算所有通知

时间:2018-06-10 19:22:04

标签: android notifications accessibilityservice notification-listener

如何在Android中简单计算所有传入的通知?我正在尝试使用此处发布的代码: Reading notifications using AccessibilityService

我也知道NotificationListenerService https://developer.android.com/reference/android/service/notification/NotificationListenerService但我真的不知道如何使用这个类来检测传入的通知。

我非常感谢您提供的任何帮助!

MainActivity

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

public class MainActivity extends AppCompatActivity {

    private static MainActivity ins;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ins=this;

    }
    public static MainActivity getIns()
    {
        return ins;
    }

    public void updateUI(final String s)
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView textView=(TextView) findViewById(R.id.textView);
                textView.setText(s);
            }
        });
    }
}

NotificationAccessibilityService

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.os.Parcelable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

public class NotificationAccessibilityService extends AccessibilityService{
    int notification=0;
    protected void onServiceConnected() {
        Log.d("Tortuga", "AccessibilityService Connected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        setServiceInfo(info);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent e) {
        Log.d("Tortuga","FML");
        if (e.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {

            notification++;

            Log.d("Tortuga","Recieved event");
            Parcelable data = e.getParcelableData();
            if (data instanceof Notification) {
                Log.d("Tortuga","Recieved notification");
                Notification notification = (Notification) data;
                Log.d("Tortuga","ticker: " + notification.tickerText);
                Log.d("Tortuga","icon: " + notification.icon);
                Log.d("Tortuga", "notification: "+ e.getText());
            }
        }

        MainActivity.getIns().updateUI(Integer.toString(notification));
    }

    @Override
    public void onInterrupt() {
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    tools:context="com.example.ivan.notifications.MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ivan.notifications">
    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.ivan.notifications.NotificationAccessibilityService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
                <intent-filter>
                    <action android:name="android.accessibilityservice.AccessibilityService" />
                </intent-filter>
            <meta-data
                android:name=".MainActivity"
                android:resource="@layout/activity_main" />
        </service>
    </application>
</manifest>

@@@编辑: 我犯了以下错误:

FATAL EXCEPTION: main
                                                                              Process: com.example.ivan.notifications, PID: 9809
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ivan.notifications.MainActivity.updateUI(java.lang.String)' on a null object reference
                                                                                  at com.example.ivan.notifications.NotificationAccessibilityService.onAccessibilityEvent(NotificationAccessibilityService.java:39)
                                                                                  at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent(AccessibilityService.java:1538)
                                                                                  at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:1724)
                                                                                  at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6656)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

0 个答案:

没有答案