2012-10-26 16 views
0

我的Manifest.xml:結束呼叫()函數這麼想的工作

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

我可以捕獲所有的收入呼叫號碼,並在我的數據庫保存它。但在一些手機中,函數endCall()不起作用。鍾仍響。我怎麼解決這個問題?謝謝。

public class CallReceiver extends BroadcastReceiver { 

    public static final String TEST_NUMBER = "62419770"; 

    private static final String ACTION = "android.intent.action.PHONE_STATE"; 

    private ITelephony iTelephony; 
    private AudioManager mAudioManager; 

    public void onReceive(Context context, Intent intent) { 
     mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
     TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 

     try { 
      Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null); 
      getITelephonyMethod.setAccessible(true); 
      iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyMgr,(Object[]) null); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     String action = intent.getAction(); 

     if (ACTION.equals(action)) { 
      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
      String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
      if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) { 
       if(NameListData.hasCallName(number,DataBase.NameRecord.TYPE_BLACK)) 
       { 
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
        try { 
         iTelephony.endCall(); 
         mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
         NotificationUtils.showSingleNotification(context, 
           context.getString(R.string.notification_intercept_phone_title), 
           number, InterceptListActivity.class,NotificationUtils.mInterceptrCallNotificationId);  
         HarassCallUtils.addHarassCall(context, new CallItem(0, number, new Date().getTime(), 0 ,false)); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        }      
       } 
      } 
     } 
    } 
} 

回答

4

儘量使用耳機的功能是這樣的:

public class CallReceiver extends BroadcastReceiver { 
... 
    try { 
     endCallAidl(context); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     Log.d("EndCall","Error trying to end call using telephony service. Falling back to headset."); 
      endPhoneHeadsethook(context); 
    } 
... 
} 

private void endPhoneHeadsethook(Context context) { 
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG); 
    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 
    String origState = headSetUnPluggedintent.getStringExtra("state"); 
    headSetUnPluggedintent.putExtra("state", 1); 
    headSetUnPluggedintent.putExtra("name", "Headset"); 

    // TODO: Should we require a permission? 
    sendOrderedBroadcast(headSetUnPluggedintent, null); 

    Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);    
    buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
    getBaseContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED"); 


    // froyo and beyond trigger on buttonDown instead of buttonUp 
    Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);    
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
    context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED"); 

    if (origState != "1"){ 
     headSetUnPluggedintent.putExtra("state", 0); 
     sendOrderedBroadcast(headSetUnPluggedintent, null); 
    } 
} 

@SuppressWarnings({ "unchecked", "rawtypes" }) 
private void endCallAidl(Context context) throws Exception { 

     // Set up communication with the telephony service (thanks to Tedd's Droid Tools!) 
     TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
     Class c = Class.forName(tm.getClass().getName()); 
     Method m = c.getDeclaredMethod("getITelephony"); 
     m.setAccessible(true); 
     ITelephony telephonyService; 
     telephonyService = (ITelephony)m.invoke(tm); 

     // end the call! 
     telephonyService.endCall(); 
}