0

我的 Manifest.xml:</p>

<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

1 回答 1

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(); 
}
于 2012-10-29T12:15:42.287 回答