How to Reject a call programatically in android

后端 未结 2 1150
悲哀的现实
悲哀的现实 2020-12-02 19:49

In my app I will maintain a list of contacts.

Any calls from contacts in the list will be dropped. They will show under missed calls but the phone will not ring.

2条回答
  •  独厮守ぢ
    2020-12-02 20:17

    First create this Interface:

      public interface ITelephony {
    
            boolean endCall();
    
            void answerRingingCall();
    
            void silenceRinger();
    
      }
    

    Then Create this class that extends BroadcastReceiver

    public class IncomingCallReceiver extends BroadcastReceiver {
        private ITelephony telephonyService;
        private String blacklistednumber = "+458664455";
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
           TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
           try {
             Class c = Class.forName(tm.getClass().getName());
             Method m = c.getDeclaredMethod("getITelephony");
             m.setAccessible(true);
             ITelephony telephonyService = (ITelephony) m.invoke(tm);
             Bundle bundle = intent.getExtras();
             String phoneNumber = bundle.getString("incoming_number");
             Log.e("INCOMING", phoneNumber);
             if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) { 
                telephonyService.silenceRinger();
                telephonyService.endCall();
                Log.e("HANG UP", phoneNumber);
             }
    
           } catch (Exception e) {
             e.printStackTrace();
           }
    }
    

    This will only block that single phonenumber, but you get the point.

    In your manifest add this:

    
        
            
        
    
    
    
    
    
    

提交回复
热议问题