mActions not found when trying to extract contents of notification in android 6.0.1 marsmalllow

using notification listener to access contents of notification especially for whatsapp but i am geting this error plese help

Notification listener.Class

package com.example.pavilion.notificationlistener;

/**
 * Created by pavilion on 6/9/2017.
 */
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;


public class NotificationService extends NotificationListenerService {

    Context context;
    public static boolean enabled;
    public static boolean groupSupport;

    @Override

    public void onCreate() {


        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
         enabled = sharedPrefs.getBoolean("enabled", true);
        groupSupport = sharedPrefs.getBoolean("group", true);
    }
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        if (enabled) {
            Notification mNotification = sbn.getNotification();
                    if (mNotification != null) {
                        String msg = getLastMsg(mNotification);
                        Log.e("NotificationClassifier", "New notification");
                        if (!TextUtils.isEmpty(msg) ) {
                            if (groupSupport) {
                                Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
                                return;
                            } else if (!msg.contains("@")) {
                                Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();

                                return;
                            } else {
                                Toast.makeText(this,"last option",Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        return;
                    }
                    return;
                }
        else {
            Toast.makeText(this,"WhatsApp in foreground ",Toast.LENGTH_SHORT).show();
            return;
            }
    }


    @Override

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

    }

    public static String getLastMsg(Notification notification) {
        RemoteViews views = notification.bigContentView;
        if (views == null) {
            views = notification.contentView;
        }
        if (views == null) {
            return null;
        }
        String text = "Message From ";
        try {
            Field field = views.getClass().getSuperclass().getDeclaredField("mActions");
            field.setAccessible(true);
            ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field.get(views);


            for (Parcelable p : actions) {
                Parcel parcel = Parcel.obtain();
                p.writeToParcel(parcel, 0);
                parcel.setDataPosition(0);
                if (parcel.readInt() == 2) {
                    parcel.readInt();
                    String methodName = parcel.readString();
                    if (methodName == null) {
                        continue;
                    } else {
                        if (methodName.equals("setText")) {
                            parcel.readInt();
                            Iterator it = parcel.createStringArrayList().iterator();
                            while (it.hasNext()) {
                                String s = (String) it.next();
                                if (!(TextUtils.isEmpty(s) || s.contains("1 new message"))) {
                                    text = text + "  " + s + ": ";
                                    if (s.indexOf("messages") >= 0) {
                                        parcel.recycle();
                                        return null;
                                    }
                                    Log.i("Parcel content", BuildConfig.FLAVOR + s);
                                }
                            }
                        }
                        parcel.recycle();
                    }
                }
            }
            return text;
        } catch (Exception e) {
            Log.e("Notification error", e.toString());
            return text;
        }
    }
}

MainActivity.java

package com.example.pavilion.notificationlistener;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.app.Activity;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.graphics.Color;
        import android.os.Bundle;
        import android.support.v4.content.LocalBroadcastManager;
        import android.text.Html;
        import android.widget.TableLayout;
        import android.widget.TableRow;
        import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TableLayout tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        NotificationService.enabled = sharedPrefs.getBoolean("enabled", true);

    }


}

AndroidMAnifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pavilion.notificationlistener">
<uses-permission android:name="android.permission..ACCESS_NOTIFICATION_POLICY"></uses-permission>
    <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=".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>

Below is the error i am getting my device is Moto G3 android 6.0.1 marshmallow

06-12 08:20:21.506 1949-1964/com.example.pavilion.notificationlistener E/Notification error: java.lang.NoSuchFieldException: No field mActions in class Ljava/lang/Object; (declaration of ‘java.lang.Object’ appears in /system/framework/core-libart.jar)

06-12 08:20:21.506 1949-1964/com.example.pavilion.notificationlistener E/NotificationClassifier: New notification
06-12 08:20:21.507 1949-1964/com.example.pavilion.notificationlistener W/NotificationListenerService[NotificationService]: Error running onNotificationPosted
                                                                                                                           java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                                                                               at android.os.Handler.<init>(Handler.java:200)
                                                                                                                               at android.os.Handler.<init>(Handler.java:114)
                                                                                                                               at android.widget.Toast$TN.<init>(Toast.java:345)
                                                                                                                               at android.widget.Toast.<init>(Toast.java:101)
                                                                                                                               at android.widget.Toast.makeText(Toast.java:259)
Miner Asked on June 12, 2017 in General Stack.
Add Comment


  • LATEST ANSWERS

  • 1 Answer(s)

    I am also facing same problem. Did you find the solution.  my device is android  6.0.1(marshmallow)
    If you find solution please let me know

    Default Answered on June 12, 2017.
    Add Comment

    Write your answer

    By posting your answer, you agree to the privacy policy and terms of service.