Programmatically accept call in Nougat

前端 未结 4 2119
孤街浪徒
孤街浪徒 2020-12-24 09:04

From one year, I have been working over IOT product and the application attached was working fine. Now I am not able to accept call programmatically in higher versions of an

4条回答
  •  轮回少年
    2020-12-24 09:11

    For answering a call using a button, set a flag whenever an incoming call is detected:

     if(state==TelephonyManager.CALL_STATE_RINGING){
               shouldAnswerCallViaNotification = true;
            } else {
                shouldAnswerCallViaNotification = false;
            }
    

    Now, create a list in your NSLogService class,

    static ArrayList statusBarNotifications;
    

    and in your onNotificationPosted() add StatusBarNotification to a list,

       @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    
        if (HomeScreen.shouldAnswerCallViaNotification) {
            if (statusBarNotifications == null) {
                updateNotificationList();
            }
           statusBarNotifications.add(sbn);
    
        } else {
            updateNotificationList();
        }
    
    }
     public static ArrayList getAllNotifications() {
        return statusBarNotifications;
    }
    
    public static void updateNotificationList() {
    
        if (statusBarNotifications != null)
            statusBarNotifications = null;
        statusBarNotifications = new ArrayList();
    }
    

    In your HomeScreen, on Button click, call performNotificationOperation(NLService.getAllNotifications());

    here is a definition for this method:

     private void performNotificationOperation(ArrayList activeNotifications) {
    
        if (activeNotifications.size()> 0) {
            main_Loop:
            for (StatusBarNotification notification : activeNotifications) {
                try {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        if (notification.getNotification().actions != null) {
                            for (Notification.Action action : notification.getNotification().actions) {
                                //            Log.e(TAG, "" + action);
    
                                Log.e(TAG, "" + action.title);
                                if (action.title.toString().equalsIgnoreCase("Answer")) {
                                    Log.e(TAG, "" + true);
                                    PendingIntent intent = action.actionIntent;
    
                                    try {
                                        intent.send();
                                    } catch (PendingIntent.CanceledException e) {
                                        e.printStackTrace();
                                    }
                                    break main_Loop;
                                }
    
    
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        }
        try {
            NLService.updateNotificationList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

提交回复
热议问题