简体   繁体   中英

Click “Exit” button of Google Navigation notification from my application

I want to "click" the "Exit Navigation" button that appear in the Google Navigation notification from my android application code.

Google Navigation notification: 在此处输入图片说明

I know that there is a way to do it because I can do it using a Tasker task, but I don't know how to do it using code from my application.

I have a NotificationListenerService and I can retrieve the text of the notification when onNotificationPosted is received with the following code:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Notification notification = sbn.getNotification();

    if (!pack.equals("com.google.android.apps.maps")) return;

    RemoteViews views = notification.bigContentView;
    if (views == null) views = notification.contentView;
    if (views == null) return null;

    int viewid=0;

    List<String> text = new ArrayList<String>();
    try
    {
        Field fieldActions = views.getClass().getDeclaredField("mActions");
        fieldActions.setAccessible(true);

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

        for (Parcelable p : actions)
        {
            Parcel parcel = Parcel.obtain();
            p.writeToParcel(parcel, 0);
            parcel.setDataPosition(0);

            int tag = parcel.readInt();

            if (tag != 2) continue;

            viewid=parcel.readInt();

            String methodName = parcel.readString();
            if (methodName == null) continue;
            else{
                if (methodName.equals("setText"))
                {
                    parcel.readInt();
                    String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim();
                    text.add(t);
                    Log.w(TAG,"viewID: "+viewid+"setText: "+t);
                }
            }

            parcel.recycle();
        }
    } catch (Exception e)
    {
        Log.e(TAG, e.toString());
    }

    return text;
}

But I didn't find a way to retrieve the actions ("buttons") of the notification.

Thanks!

It's quite simple. Just get the Action Buttons from that notification like this.

Notification notification = sbn.getNotification();
Notification.Action[] actions = notification.actions;

And because this notification has only one action button, you simply do:

actions[0].actionIntent.send();

AFTER you checked that's a Google Maps notification. You did this here:

if (!pack.equals("com.google.android.apps.maps")) return;



So complete code should look like this:

Notification notification = sbn.getNotification();
Notification.Action[] actions = notification.actions;    

if (!pack.equals("com.google.android.apps.maps")) return;    
actions[0].actionIntent.send();

Should work. Hope I can help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM