2017-04-26 70 views
1

我想阻止Android使用此方法的传入呼叫,但通过 这种异常。我已经尝试了这么多的方法,但未能中止来电,请帮助这个代码的问题。我还在com.android.internal.telephony.ITelephony中添加了ITelephony接口。在Android异常来电阻止

异常

enter image description here 代码

String Messageofenemy; 
String Blckno; 

public OutgoingReceiver() { 
} 


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

    try { 


     if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { 


      Intent i = new Intent(context, MyService.class); 
      context.startService(i); 
      return; 
     } 


     if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 

      //outgoing call code here 

     } else { 
      //get the phone state 
      String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? 
        intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null; 
      Bundle bundle = intent.getExtras(); 
      if (newPhoneState != null && 
        newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
       //read the incoming call number 
       String phoneNumber = 
         bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 

       db = new DatabaseHelper(context); 
       Cursor res = db.getAllrows(); 
       if (res.getCount() == 0) { 

       } else { 

        while (res.moveToNext()) { 

         Blckno = res.getString(0); 

        } 


       } 
       if (phoneNumber.equals("03352264769")) { 

        try { 

         AudioManager am = 
           (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
         am.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
         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); 
          telephonyService = (ITelephony) m.invoke(telephony); 
          telephonyService.endCall(); 
         } catch (Exception e) { 
          Toast.makeText(ctx, "Exception" + String.valueOf(e), 
            Toast.LENGTH_LONG).show(); 

         } 


        } catch (Exception e) { 
         Toast.makeText(context, "Exception is " + String.valueOf(e), 
           Toast.LENGTH_LONG).show(); 
        } 
       } 


      } 


     } 


    } catch (Exception ex) { 

     Toast.makeText(context, "Error is " + String.valueOf(ex), 
       Toast.LENGTH_LONG).show(); 

    } 


} 


} 
+0

请格式化您的代码 – Jens

+0

我已格式化请检查。 – DumpsterDiver

+0

看起来像'context'为空 – Jens

回答

0

检查通话状态和onReceive()呼入号码。然后使用disconnectPhoneItelephony(Context context)中止来电 -

public class OutgoingReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) {  
       if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
          TelephonyManager.EXTRA_STATE_RINGING)) { 
         if (!intent.getAction().equals("android.intent.action.PHONE_STATE") || intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
          Toast.makeText(context, "Not phone state", Toast.LENGTH_LONG).show(); 
          //return; 

          // Else, try to do some action 
         } else { 
          if (number == null) { //get the incomming call number 
           number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
      disconnectPhoneItelephony(context); // this is method whick blocks incoming call, this method is provided on the down 
          } 
      } 
      } 
      } 





    // Method to disconnect phone automatically and programmatically 
      // Keep this method as it is 
      @SuppressWarnings({ "rawtypes", "unchecked" }) 
      private void disconnectPhoneItelephony(Context context) { 
       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 disconect call...."); 

       } 


      } 



     } 

这里是接口ITelephony.java -

interface ITelephony { 

    boolean endCall(); 

} 

不要忘记对您的BroadcastReceiver类上的manifest.xml文件中添加权限和意图过滤器 -

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

    <receiver 
       android:name=".OutgoingReceiver" 
       android:enabled="true"> 
       <intent-filter android:priority="9999"> 
        <action android:name="android.intent.action.PHONE_STATE" /> 
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" 
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/> 

       </intent-filter> 
      </receiver> 
+0

抱歉java.lang.reflect.InvocatioTarget异常现在抛出。 – DumpsterDiver

+1

首先尝试阻止所有呼叫。我的意思是尝试不访问DBHelper。检查阻塞是否适用于所有呼叫。我开发了一个黑名单应用程序,并在所有设备和版本上工作正常。我只是从我的应用程序复制/粘贴此代码。所以它应该工作。 –

+1

哦。还有一个版本我忘了更改清单上的接收器类。接收者应该是你的班级“OutgoingReceiver”。我正在回答一个更多的版本。 –