2013-02-19 66 views
2

我試圖在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(); 

} 
+0

我已經參加了一個關於Android的反思和具體的例子當然因爲它是編程結束通話,又名你不能沒有使用反射做(這意味着塔蘭的答案是可能是正確的) – EpicPandaForce 2014-07-01 14:07:01

回答

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>