2012-09-06 57 views
0

我必須執行創建Android應用程序的任務,該應用程序將選取所有呼叫,向對方播放預先錄製的語音並切斷電話。可能嗎?在android中自動應答

骨架代碼是最受歡迎的。

+0

是的,我認爲它不可能在android中。 – Praveenkumar

+0

[在語音通話中播放音頻文件]的可能重複(http://stackoverflow.com/questions/7042742/play-an-audio-file-on-a-voice-call) – Praveenkumar

+0

我得到了一個link.please檢查這個http://code.google.com/p/voicemail-example-for-android/wiki/Development – user189942

回答

0

不,您不能通過揚聲器回放其他任何方式。因此,您將無法編寫「無聲自動應答」應用程序或類似的東西。這是故意的我想,不要讓應用程序「說話」的代表用戶的方式,他不會知道

+0

我寫了一個示例代碼,但不知道該怎麼辦。請檢查上面的代碼 – user189942

+0

有可能向主叫方播放tts句子。這是一個考驗的事實。 (galaxy s 2 i9100)建議讓用戶輸入句子。 –

0

這是我寫的代碼:

public class AutoAnswerIntentService extends IntentService { 

     public AutoAnswerIntentService() { 
       super("AutoAnswerIntentService"); 
     } 

     @Override 
     protected void onHandleIntent(Intent intent) { 
       Context context = getBaseContext(); 

       // Load preferences 
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
       BluetoothHeadset bh = null; 
       if (prefs.getBoolean("headset_only", false)) { 
         bh = new BluetoothHeadset(this, null); 
       } 

       // Let the phone ring for a set delay 
       try { 
         Thread.sleep(Integer.parseInt(prefs.getString("delay", "2")) * 1000); 
       } catch (InterruptedException e) { 
         // We don't really care 
       } 

       // Check headset status right before picking up the call 
       if (prefs.getBoolean("headset_only", false) && bh != null) { 
         if (bh.getState() != BluetoothHeadset.STATE_CONNECTED) { 
           bh.close(); 
           return; 
         } 
         bh.close(); 
       } 

       // Make sure the phone is still ringing 
       TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
       if (tm.getCallState() != TelephonyManager.CALL_STATE_RINGING) { 
         return; 
       } 

       // Answer the phone 
       try { 
         answerPhoneAidl(context); 
       } 
       catch (Exception e) { 
         e.printStackTrace(); 
         Log.d("AutoAnswer","Error trying to answer using telephony service. Falling back to headset."); 
         answerPhoneHeadsethook(context); 
       } 

       // Enable the speakerphone 
       if (prefs.getBoolean("use_speakerphone", false)) { 
         enableSpeakerPhone(context); 
       } 
       return; 
     } 

     private void enableSpeakerPhone(Context context) { 
       AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
       audioManager.setSpeakerphoneOn(true); 
     } 

     private void answerPhoneHeadsethook(Context context) { 
       // Simulate a press of the headset button to pick up the call 
       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"); 

       // froyo and beyond trigger on buttonUp instead of buttonDown 
       Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);    
       buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
       context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED"); 
     } 

     @SuppressWarnings("unchecked") 
     private void answerPhoneAidl(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); 

       // Silence the ringer and answer the call! 
       telephonyService.silenceRinger(); 
       telephonyService.answerRingingCall(); 
     } 
}