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 に答える