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) {
}
}
但是在API级别21中不推荐使用函数speak(String text, int queueMode, HashMap params)
。我该如何解决这个问题?
答案 0 :(得分:-1)
是的,您已经过时,不推荐使用,直到您过去使用 API 21 :
public int speak (String text, int queueMode, HashMap<String, String> params)
但此方法已在 API 21 中弃用。 从API级别21开始,替换为:
public int speak (CharSequence text, int queueMode, Bundle params, String utteranceId)
在你的情况下:
...
tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null, null);
只需将null
传递给utteranceId
参数