如何在棒棒糖版本的状态栏中提取通知文本?

时间:2015-01-06 14:02:54

标签: android notifications android-5.0-lollipop

亲爱的所有我使用下面附带的代码来提取通知文本。但它在android(v 5)的棒棒糖版本中不起作用。你能帮我解决问题吗

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import de.ecspride.reactor.poc.model.Message;
import de.ecspride.reactor.poc.model.MessageParser;

@SuppressLint("NewApi")
public class DefaultBigView implements MessageParser {
    private static final String TAG = DefaultBigView.class.getName();

    private static final int ID_FIRST_LINE = 16909023; // bigContentView

    @Override
    public Message parse(Notification notification) {
        // use simple method if bigContentView is not supported
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            return new DefaultView().parse(notification);

        Message result = new Message();

        try {
            RemoteViews views = notification.bigContentView;

            Class<?> rvClass = views.getClass();

            Field field = rvClass.getDeclaredField("mActions");
            field.setAccessible(true);

            @SuppressWarnings("unchecked")
            ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field
                    .get(views);

            for (Parcelable action : actions) {
                try {
                    // create parcel from action
                    Parcel parcel = Parcel.obtain();
                    action.writeToParcel(parcel, 0);
                    parcel.setDataPosition(0);

                    // check if is 2 / ReflectionAction
                    int tag = parcel.readInt();
                    if (tag != 2)
                        continue;

                    int viewId = parcel.readInt();

                    String methodName = parcel.readString();
                    if (methodName == null || !methodName.equals("setText")) {
                        Log.w(TAG, "#Big Not setText: " + methodName);
                        continue;
                    }

                    // should be 10 / Character Sequence, here
                    parcel.readInt();

                    // Store the actual string
                    String value = TextUtils.CHAR_SEQUENCE_CREATOR
                            .createFromParcel(parcel).toString();

                    Log.d(TAG, "Big viewId is " + viewId);
                    Log.d(TAG, "Big Found value: " + value);

                //  if (viewId == ID_FIRST_LINE) {
                        int indexDelimiter = value.indexOf(':');

                        if (indexDelimiter != -1) {
                            result.sender = value.substring(0, indexDelimiter);
                            result.message = value
                                    .substring(indexDelimiter + 2);
                        }
                //  }

                    parcel.recycle();
                } catch (Exception e) {
                    Log.e(TAG, "Big Error accessing object!", e);
                }
            }

            if (result.sender == null || result.message == null)
                return null;

            return result;
        } catch (Exception e) {
            Log.e(TAG, "Big Could not access mActions!", e);

            return null;
        }
    }
}

它给出了错误,即没有声明的变量mActions。

由于

1 个答案:

答案 0 :(得分:3)

Android 5 Lollipop通知不再直接使用RemoteViews类,它们现在使用扩展RemoteViews类的BuilderRemoteViews

您应该尝试像这样

访问父类mActions字段
Field field = rvClass.getSuperclass().getDeclaredField("mActions");