2017-04-26 2 views
1

이 메소드를 사용하여 Android에서 수신 전화를 차단하려고 시도하지만이 코드는 입니다. 많은 메소드를 시도했지만 수신 호출을 중단하지 못하면이 코드의 문제점을 해결하십시오. 또한 내부에 ITelephony 인터페이스를 추가했습니다 com.android.internal.telephony.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'는 null입니다. – Jens

답변

0

확인 전화 상태 및 onReceive()에 수신 번호. 인터페이스ITelephony.java입니다 여기에

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

       } 


      } 



     } 

을 - - 그리고 걸려 오는 전화를을 중단 disconnectPhoneItelephony(Context context)를 사용

interface ITelephony { 

    boolean endCall(); 

} 

그리고 당신의 manifest.xml 파일에 BroadcastReceiver 클래스에 대한 사용 권한 및 텐트 필터를 추가하는 것을 잊지 말아 -

<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에 액세스하지 않고 사용해보십시오. 모든 통화에 대해 차단이 작동하는지 확인하십시오. 나는 모든 장치와 버전에서 Blacklist 앱과 그 잘 작동하는 앱을 개발했다. 난 그냥 내 응용 프로그램 에서이 코드를 복사/붙여 넣기. 그렇게하면 효과가 있습니다. –

+1

오. 내가 매니페스트에서 수신기 클래스를 변경하는 것을 잊어 버린 또 하나의 에디션. 수령인은 "OutgoingReceiver"클래스 여야합니다. 내 대답에 대해 하나 더 편집하고 있습니다. –