Rejecting Incoming call in android

前端 未结 6 483
旧巷少年郎
旧巷少年郎 2020-12-01 02:37

I want to reject incoming in android, I have seen so many code from these links.

  1. Android: Taking complete control of phone(kiosk mode), is it possible? How?

相关标签:
6条回答
  • 2020-12-01 02:57

    Provide appropriate permission and add the ITelephony.java file

    private void declinePhone(Context context) throws Exception {
    
            try {
    
                String serviceManagerName = "android.os.ServiceManager";
                String serviceManagerNativeName = "android.os.ServiceManagerNative";
                String telephonyName = "com.android.internal.telephony.ITelephony";
                Class<?> telephonyClass;
                Class<?> telephonyStubClass;
                Class<?> serviceManagerClass;
                Class<?> serviceManagerNativeClass;
                Method telephonyEndCall;
                Object telephonyObject;
                Object serviceManagerObject;
                telephonyClass = Class.forName(telephonyName);
                telephonyStubClass = telephonyClass.getClasses()[0];
                serviceManagerClass = Class.forName(serviceManagerName);
                serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
                Method getService = // getDefaults[29];
                serviceManagerClass.getMethod("getService", String.class);
                Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
                Binder tmpBinder = new Binder();
                tmpBinder.attachInterface(null, "fake");
                serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
                IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
                Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
                telephonyObject = serviceMethod.invoke(null, retbinder);
                telephonyEndCall = telephonyClass.getMethod("endCall");
                telephonyEndCall.invoke(telephonyObject);
    
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("unable", "msg cant dissconect call....");
    
            }
    
    0 讨论(0)
  • 2020-12-01 02:57

    If you are going to build a kiosk app then i suggest you to obtain notification settings and put device to Do not Disturb mode ON.

    var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    
    mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
    
    0 讨论(0)
  • 2020-12-01 02:58

    In order to intercept your call you just have to:
    1. Make a package named. com.android.internal.telephony;
    2. In this package make a interface file named ITelephony.
    and write this code in that interface file.

    boolean endCall();
    void answerRingingCall();
    void silenceRinger();
    

    Now in your class where you want to intercept the call extend that class to BroadcastReceiver and in onReceive()function write the following code.

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       try {
         Class c = Class.forName(tm.getClass().getName());
         Method m = c.getDeclaredMethod("getITelephony");
         m.setAccessible(true);
         telephonyService = (ITelephony) m.invoke(tm);
         Bundle bundle = intent.getExtras();
         String phoneNumber = bundle.getString("incoming_number");
         Log.d("INCOMING", phoneNumber);
         if ((phoneNumber != null)) { 
            telephonyService.endCall();
            Log.d("HANG UP", phoneNumber);
         }
    
       } catch (Exception e) {
         e.printStackTrace();
       }
    

    Thats it.

    0 讨论(0)
  • 2020-12-01 03:00

    You will be needing a broadcast receiver for that with an intent filter I think ACTION_ANSWER

    more details about it here: http://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

    You can register it using the manifest file or the method registerReceiver

    more info here: http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)

    Also you need to specify appropriate permissions in the manifest file.

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
    

    The links you have provided are quite good. You just need more patience :)

    happy coding.

    0 讨论(0)
  • 2020-12-01 03:04

    This is the best tutorial to block all incoming calls you can customize it to block incoming call of only selected numbers.

    Android Incoming Call Block

    0 讨论(0)
  • 2020-12-01 03:08

    Create an interface in com.android.internal.telephony and name as

    interface ITelephony {
        /**
         * Demonstrates some basic types that you can use as parameters
         * and return values in AIDL.
         */
        void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                double aDouble, String aString);
        boolean endCall();
        void silenceRinger();strong text
        void answerRingingCall();
    }
    
    //   then catch the incoming call intent and call below method
    
    
    
      if (intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) != null) {
                            number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                            Toast.makeText(context, "INCOMING call catched: ", Toast.LENGTH_SHORT).show();
    disconnectPhoneItelephony(mContext)
                        }
    
    
        private void disconnectPhoneItelephony(Context context) {
                ITelephony telephonyService;
                TelephonyManager telephony = (TelephonyManager)
                        context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Class c = Class.forName(telephony.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    SessionManager.getInstance(context).setBlockStatusAllow("BLOCKED");
                    telephonyService = (ITelephony) m.invoke(telephony);
                    telephonyService.silenceRinger();
                    telephonyService.endCall();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    

    Add necessary permission in Manifest

    0 讨论(0)
提交回复
热议问题