从通知

时间:2017-01-07 14:50:18

标签: android notifications whatsapp

我正在尝试从通知栏中读取传入的WhatsApp消息。

我使用以下代码来执行此操作,

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MyAccessibilityService extends AccessibilityService {

   private final AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    private static final String TAG = "MyAccessibilityService";

    public static TextView title;
    public static TextView inbox;
    public static TextView text;


    @Override
    protected void onServiceConnected()
    {
        Log.d("onServiceConnected", "ServiceConnected");
        try
        {
            AccessibilityServiceInfo info = new AccessibilityServiceInfo();

            info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
            info.packageNames = new String[]
            {"com.whatsapp"};


            info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;

            info.notificationTimeout = 100;

            setServiceInfo(info);
        }
        catch(Exception e)
        {
            Log.d("ERRORonServiceConnected", 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.contentView;

                ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null);

                remoteView.reapply(getApplicationContext(), localView);

                Resources resources = null;

                PackageManager pkm = getPackageManager();

                try
                {
                    resources = pkm.getResourcesForApplication("com.whatsapp");
                }
                catch (PackageManager.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("got x messages : ", text.getText().toString());

                Log.d("NOTIFICATION inbox : ", inbox.getText().toString());
        }
        }
        catch(Exception e)
        {
            Log.e("onAccessibilityEvent", e.toString());
        }
    }
}

收到WhatsApp消息时,它会在日志中显示以下错误

E/onAccessibilityEvent﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RemoteViews.getLayoutId()'on a null object reference

注意: 我没有添加任何特定权限。 请帮我解决错误。我对此特别陌生。

1 个答案:

答案 0 :(得分:0)

试试这个,下面的代码适合我 -

<?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" />
希望它会对你有所帮助。

在你的res文件夹中创建一个名为xml的文件夹 - 在其中创建一个名为“accessibilityservice”的xml并粘贴到代码下面 -

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

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

{{1}}