在android中拒绝来电

时间:2013-02-21 20:58:07

标签: android telephony callblocking incoming-call

6 个答案:

答案 0 :(得分:15)

为了拦截你的电话,你只需要:
1.制作一个名为的包裹。 com.android.internal.telephony;
2.在此包中创建一个名为ITelephony的接口文件 并在该接口文件中编写此代码。

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

现在,在您要拦截调用的类中,将该类扩展为BroadcastReceiver并在onReceive()函数中编写以下代码。

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();
   }

多数民众赞成。

答案 1 :(得分:3)

提供适当的权限并添加ITelephony.java文件

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

        }

答案 2 :(得分:1)

你需要一个带有意图过滤器的广播接收器我觉得ACTION_ANSWER

有关它的更多详情:http://developer.android.com/reference/android/content/Intent.html#ACTION_ANSWER

您可以使用清单文件或方法registerReceiver

进行注册

此处有更多信息:http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,android.content.IntentFilter)

您还需要在清单文件中指定适当的权限。

<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" />

您提供的链接非常好。你只需要更多的耐心:)

快乐的编码。

答案 3 :(得分:0)

com.android.internal.telephony中创建界面 并命名为

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();
            }
        }

在Manifest中添加必要的权限

答案 4 :(得分:0)

这是阻止所有来电的最佳教程,您可以对其进行自定义以阻止仅选定号码的来电。

Android Incoming Call Block

答案 5 :(得分:0)

如果您要构建信息亭应用程序,那么我建议您获取通知设置并将设备设置为“请勿打扰”模式。

var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)