Rejecting Incoming call in android

我与影子孤独终老i 提交于 2019-12-17 08:51:55

问题


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?

  2. How to Reject a call programatically in android

  3. http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html

  4. http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html?showComment=1361478035090#c5468022990217431781

But I am still unable to do it, can anybody tell me in simple and easy steps how to do it?


回答1:


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.




回答2:


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....");

        }



回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/15012082/rejecting-incoming-call-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!