安卓应用开发中使用代码接通电话

这个也是在别的地方看的,具体不记得了,自己只是做个备份,以免以后找不到。

android2.3之前的版本可以使用ITelephony.answerRingingCall()方法来接通电话,可惜后来被认为是危险的api,那个PHONE_CHANGE_STATE仅限于system用户使用,应用级的程序不能再访问这个。前些天偶然搜索到了一个帖子,能够解决使用代码接通。忘了在哪儿找的了,也没法引用转载地址了。

public void autoAnswerPhone() { 
        try {
        	TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        	Class<TelephonyManager> c = TelephonyManager.class;  
        	Method mthEndCall = null;  
        	mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);  
            mthEndCall.setAccessible(true);  
            ITelephony itelephony = (ITelephony) mthEndCall.invoke(telMag,  
                    (Object[]) null);
            //itelephony.silenceRinger(); 
            itelephony.answerRingingCall(); 
        } catch (Exception e) {
            e.printStackTrace();
            Application app = getApplication();
            try {
                Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
                KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK);
                intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent);
                
                app.sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED");
                 
                intent = new Intent("android.intent.action.MEDIA_BUTTON");
                keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
                intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent);
                getApplication().sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.sendOrderedBroadcast(intent,"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");
                getApplication().sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.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);
                app.sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.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);
                app.sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.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");
                app.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED");
                //TApplication.nowApplication.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED");
            } catch (Exception e2) {
                e2.printStackTrace();
                Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  
                KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  
                meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);  
                app.sendOrderedBroadcast(meidaButtonIntent, null);
                //TApplication.nowApplication.sendOrderedBroadcast(meidaButtonIntent, null);
                }
            }
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值