2015-12-10 80 views
0
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { 
    private TextToSpeech tts; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tts = new TextToSpeech(this,this); 

     TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 

     PhoneStateListener callStateListener = new PhoneStateListener() { 
      public void onCallStateChanged(int state, String incomingNumber) { 

       if(state== TelephonyManager.CALL_STATE_RINGING){ 
        tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null); 
        Toast.makeText(getApplicationContext(),"Phone is Ringing : "+incomingNumber,Toast.LENGTH_LONG).show(); 
       } 
      } 
     }; 
    } 

    @Override 
    public void onInit(int status) { 

    } 
} 

但功能speak(String text, int queueMode, HashMap params)已在API級別21中棄用。我該如何解決此問題?android - 如何使用TextToSpeech與API 21

回答

0

是的,你分辯,它已被棄用,直到API 21你習慣使用:

public int speak (String text, int queueMode, HashMap<String, String> params) 

但這種方法在API 21已被否決。

public int speak (CharSequence text, int queueMode, Bundle params, String utteranceId)

你的情況

: 作爲API級別21,取而代之的是

...  
tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null, null); 

只是傳遞一個nullutteranceId參數

更多信息有關Text To Speech