Android 自动接听电话

1. android 2.3以下版本(不包括2.3)
http://bbs.51cto.com/viewthread.php?tid=1078059&extra=&page=1
中的“二  android低版本自动接听/挂断实现”

核心代码:

Class<TelephonyManager> c = TelephonyManager.class;           
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyManager, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
Log.e(TAG, "Fail to accept call.", e);
}

Class<TelephonyManager> c = TelephonyManager.class;           
try {
	Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
                IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
                ITelephony telephony = ITelephony.Stub.asInterface(binder);
                telephony.answerRingingCall();
}catch (Exception e) {
                Log.e(TAG, "Fail to accept call.", e);
}

没有2.3版本以下的手机,没有测试。

2. android 2.3版本

(1)不使用耳机

	private void answerRingingCall() { 
        //插耳机
           Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
           localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
           localIntent1.putExtra("state", 1);
           localIntent1.putExtra("microphone", 1);
           localIntent1.putExtra("name", "Headset");
           sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED");
        //按下耳机按钮
           Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
           KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent2.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent1);
           sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED");
        //放开耳机按钮
           Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
           KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent3.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent2);
           sendOrderedBroadcast(localIntent3, "android.permission.CALL_PRIVILEGED");
        //拔出耳机
           Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);
           localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
           localIntent4.putExtra("state", 0);
           localIntent4.putExtra("microphone", 1);
           localIntent4.putExtra("name", "Headset");
           sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED");
    }
(2)需使用耳机(在网上很多人提供了这个方案,所以在这里贴出来)

	private void answerRingingCall() { 
        //按下耳机按钮
           Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
           KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent2.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent1);
           sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED");
        //放开耳机按钮
           Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
           KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2);
           sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED");
    }

3. android 4.1, 4.2 版本

更高版本的没有相关机器测试。

从http://blog.sina.com.cn/s/blog_6dc1188a010176cy.html获得的方案

(1) 不使用耳机

private void answerRingingCall() { 

     //放开耳机按钮
        Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK);
        localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2);
        sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED");
        
        //插耳机
           Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
           localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
           localIntent1.putExtra("state", 1);
           localIntent1.putExtra("microphone", 1);
           localIntent1.putExtra("name", "Headset");
           sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED");
        //按下耳机按钮
           Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
           KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent2.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent1);
           sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED");
        //放开耳机按钮
            localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
            localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK);
           localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2);
           sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED");
        //拔出耳机
           Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);
           localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
           localIntent4.putExtra("state", 0);
           localIntent4.putExtra("microphone", 1);
           localIntent4.putExtra("name", "Headset");
           sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED");
    }
(2)使用耳机同上面的2(2)


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值