首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在手机android4.0中实现自动应答功能?

如何在手机android4.0中实现自动应答功能?
EN

Stack Overflow用户
提问于 2013-01-05 16:03:00
回答 1查看 956关注 0票数 0

我想设置一个函数,让它可以自动接收android 4.0版本以上的呼叫。代码如下:

代码语言:javascript
复制
synchronized void autoAnswerCall(){

  Context context = TApplication.nowApplication;

   try

         {

          //insert earphones

             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");

             context.sendOrderedBroadcast(localIntent1, "android.permission.CALL_PRIVILEGED");


             //Press the headset button

             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);

             context.sendOrderedBroadcast(localIntent2, "android.permission.CALL_PRIVILEGED");


             //Open the headset button

             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);

             context.sendOrderedBroadcast(localIntent3, "android.permission.CALL_PRIVILEGED");


             //Pull out earphones

             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");

             context.sendOrderedBroadcast(localIntent4, "android.permission.CALL_PRIVILEGED");

         }catch (Exception e){

             e.printStackTrace();

         }

}

在模拟器和手机的android2.3中实现了自动应答功能。它也可以在android4.0模拟器中运行。但在android4.0手机上就不行了。

我用的是aidl反射,它不能。如何在手机android4.0中实现自动应答功能?有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2013-01-05 17:31:53

您可以使用电话管理器接收您的振铃呼叫,为此,您必须将此代码放入广播接收器中

代码语言:javascript
复制
 if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {          
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
            Class c = null;
                 c = Class.forName(telephony.getClass().getName());
                 Method m = null;
                 m = c.getDeclaredMethod("getITelephony");
                 m.setAccessible(true);              
                telephonyService = (ITelephony) m.invoke(telephony);
                telephonyService.answerRingingCall();

       }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14169899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档