拨打来电

时间:2013-02-19 20:49:24

标签: java android android-broadcast

我正在尝试阻止android中的来电。我有这个BroadcastReceiver,但它处理传入的呼叫,但不阻止我的Android 2.3.6手机上的来电(没有尝试其他版本)。 这是我的接收者:

public class PhoneCallReceiver extends BroadcastReceiver {
    Context context = null;
    private static final String TAG = "Phone call";
    private ITelephony telephonyService;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "Receving....");

        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Class c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(telephony);
            // telephonyService.silenceRinger();

            telephonyService.endCall();
        } catch (Exception e) {
            Log.v(TAG, "failed....");
            e.printStackTrace();
        }    
    }    
}

和ITelephony

package com.callblocker.mk;

interface ITelephony {

  boolean endCall();

  void answerRingingCall();

  void silenceRinger();

}

1 个答案:

答案 0 :(得分:1)

在广播接收器中调用此方法

 public static void disconnectPhoneItelephony(Context context) {
     ITelephony telephonyService;
     Log.v(TAG, "Now disconnecting using ITelephony....");
      TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {
          Log.v(TAG, "Get getTeleService...");
          Class c = Class.forName(telephony.getClass().getName());
          Method m = c.getDeclaredMethod("getITelephony");
          m.setAccessible(true);
          telephonyService = (ITelephony) m.invoke(telephony);
            telephonyService.endCall();
      } catch (Exception e) {
       e.printStackTrace();
       Log.e(TAG,
               "FATAL ERROR: could not connect to telephony subsystem");
       Log.e(TAG, "Exception object: " + e);
      }
 }

//广播接收器

 @Override
public void onReceive(Context context, Intent intent) {


    if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
         return;
     else {

         disconnectPhoneItelephony(context);
}}

//清单

 <!-- BLOCK CALL -->
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />

<receiver android:name="receiver.CallReceiver" >
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
     </receiver>