简体   繁体   中英

How to realize the Auto answer function in android4.0 of mobile phone?

I want to set a function that it can receive the call automatically above the android 4.0 versions. The code like this:

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

         }

}

It realized the Auto answer function in android2.3 of simulator and mobile phone. It also can in android4.0 simulator. But in android4.0 mobile phone it can not.

I use aidl reflex, it can not. How to realize the Auto answer function in android4.0 of mobile phone? Any idea?

You can use Telephony manager to receive your ringing call,For this you have to put this code in Broadcast receiver

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

       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM