0

在 NotificationListener 服务中,当我们通过 onNotificationPosted 获得通知时,或者您可以说 Notification.Actions[] 我可以通过 action.send() 获得操作并单击它们,但在这种情况下,这些不是此通知中的操作按钮图片我需要点击它们 我如何点击它们。

简单来说——

我可以点击通知操作按钮 我可以点击

This is the Code
Notification n = sbn.getNotification();
Notification.Action[] actions = n.actions;
actions[0].actionIntent.send(); // Working fine

完整的 NotificationListener 代码

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    String packageName = sbn.getPackageName();
    //Log.d("NOTIFS", ".........."+packageName);
    String dlist = getPackagesOfDialerApps(getApplicationContext()).toString(); // LIST of all dialer/phone app packages
    //Log.d("NOTIFS DIALERS ..........", ""+dlist);
    if(packageName.equalsIgnoreCase(dlist) || dlist.contains(packageName) || dlist.contains("phone") || dlist.contains("dialer") || dlist.contains("call")){
        sp = getSharedPreferences("Oncall", MODE_PRIVATE);
        esp = sp.edit();
        Log.d("NOTIFS", ".......... Incoming Call");
        Notification n = sbn.getNotification();
        final Notification.Action[] actions = n.actions;
        if(actions != null) {
            Log.d("NOTIFS ..............................", ""+actions.toString() + " LENGTH......" + actions.length);
            for(int x=0; x<actions.length; x++){
                String t1 = actions[x].title.toString();
                Log.d("NOTIFS ACTION "+x, "............." + t1);
                if (t1.contains("Answer") || t1.contains("Accept")) {
                    Log.d("NOTIFS CATCH", "............. FOUND ANSWER BUTTON on ACTION - "+x);
                    Toast.makeText(this, "Answering Call in 5 seconds", Toast.LENGTH_SHORT).show();
                    final int finalX = x;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Log.d("NOTIFS", ".......... Call Answered");
                                actions[finalX].actionIntent.send();
                                esp.putBoolean("mycall", true);
                                esp.apply();
                                Toast.makeText(getApplicationContext(), "Call Answered", Toast.LENGTH_SHORT).show();
                            } catch (PendingIntent.CanceledException e) {
                                e.printStackTrace();
                            }
                        }
                    }, 5000);

                }else if(t1.contains("Hang up") || t1.contains("End call")){
                    Log.d("NOTIFS", ".......... END Call Initiator");
                    if(sp.getBoolean("mycall", false)){
                        Log.d("NOTIFS", ".......... ON Call already");
                        //END CALL WITH TIMER
                        Toast.makeText(this, "Call will End in 10 seconds.", Toast.LENGTH_SHORT).show();
                        final int finalX1 = x;
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    actions[finalX1].actionIntent.send();
                                    Log.d("NOTIFS", ".......... Call End");
                                }catch (PendingIntent.CanceledException e) {
                                    e.printStackTrace();
                                    Log.d("NOTIFS", ".......... Can;t end call");
                                }
                            }
                        }, 10000);
                        //END CALL WITH TIMER END
                    }
                }else{
                    Toast.makeText(this, "NO DEFINED ACTION FOUND", Toast.LENGTH_SHORT).show();
                    Log.d("NOTIFS ", ".............  ACTIONS "+x+"........."+t1 );
                }
            }

            
        }else{
            Toast.makeText(this, "Call answer action not found in notification", Toast.LENGTH_SHORT).show();
            Log.d("NOTIFS ", ".............  NO ACTION FOUND");

        }
    }

}

我想点击这些类型的通知按钮 我想点击的按钮/图像

4

0 回答 0