Programmatically accept call in Nougat

前端 未结 4 2130
孤街浪徒
孤街浪徒 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:34

    As I am also working on IOT product, this was one of the biggest issue I faced but after some Research, I think I have found some solution for this problem, or you can say a simple hack. I have tested this hack in several devices with several versions, and found that most of the devices are responding. Only Samsung devices are not responding, some Huawei devices and some Oppo devices are also not responding.(I am still looking something for these devices too).

    I noticed that Android provides one feature of Accessing Notifications. You can use NotificationListenerService to read notifications and perform some actions over them. It provides some override methods:

     onNotificationPosted()
        onNotificationRemoved()
        getActiveNotifications()
    

    ... etc

    Here is a code: Create a Service that extends NotificationListenerService

     class NLService extends NotificationListenerService {
    
         @Override
         public void onNotificationPosted(StatusBarNotification sbn) {
           ....
         }
    
         @Override
         public void onNotificationRemoved(StatusBarNotification sbn) {
           ....
         }
    

    In AndroidMenifest, add this service as:

     
            
                
            
        
    

    This will allow your application to read any notification received.

    Now, here is the main code:

    In onNotificationPosted(StatusBarNotification sbn) add this code:

     @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
            try {
                if (sbn.getNotification().actions != null) {
                    for (Notification.Action action : sbn.getNotification().actions) 
                      {
                        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();
                            }
                        }
                    }
                }
              } catch (Exception e) {
                  e.printStackTrace();
              }
         }
    

    Thats it!

    All is set, Run the application and the devices except Samsung, whichever shows a notification for Incoming call, with Answer and Reject/Decline Action buttons, will allow you to answer a call.

    To open Notification Access Settings and allowing your application to read notification, use:

     Intent intent = new 
        Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(intent); 
    

    Just create a POC for this and let me know about how it works.

    Please mark my answer if this helps.

    Also, if you could provide some solution for the same regarding Samsung Devices, please update.

    Thanks

提交回复
热议问题