[英]How can I create a TextToSpeech object in an Activity called from the broadcast receiver?
I am new to android.I am trying to create a caller name announcer app. 我是android的新手,我正在尝试创建一个呼叫者姓名播音员应用程序。 Following is my code.
以下是我的代码。 The logs show that the code is executing just fine ie the TextToSpeech constructor is executing and reaching the onInit method till the very end, however, when I test it, the audio is not heard.
日志显示代码执行得很好,即TextToSpeech构造函数正在执行并到达onInit方法直到最后,但是,当我对其进行测试时,听不到声音。
This is my CustomBroadcastReceiver.java: 这是我的CustomBroadcastReceiver.java:
public class CustomBroadcastReceiver extends BroadcastReceiver {
protected String newSender;
@Override
public void onReceive(Context context, Intent intent) {
Log.v("", "logger WE ARE INSIDE!!!!!!!!!!!");
Intent i=new Intent(context,ReceiverActivity.class);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr = bundle.getString("incoming_number");
Log.v("", "logger phoneNr: " + phoneNr);
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNr));
Cursor cursor = context.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME }, phoneNr, null, null);
if (cursor.moveToFirst()) {
newSender = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
i.putExtra("ID", newSender);
Log.d("","logger intent" + i.getStringExtra("ID"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
cursor.close();
}
}
This is ReceiverActivity.java 这是ReceiverActivity.java
public class ReceiverActivity extends Activity implements TextToSpeech.OnInitListener{
String name;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
Log.d("","logger reached ReceiverActivity");
tts = new TextToSpeech(this, this);
Log.d("","logger created tts object");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.receiver, menu);
return true;
}
@Override
public void onInit(int status) {
Log.d("","logger reached onInit");
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US); // java.Util.Locale for
// country codes
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
tts.speak(name, TextToSpeech.QUEUE_FLUSH, null);
Log.d("","logger speak executed");
}
}
Could you tell me where I am going wrong? 你能告诉我我要去哪里错吗?
First of all, initialize the Text-To-Speech object in your activity class, and then make static method with the string parameter, which contains speak method of Text-To-Speech. 首先,在您的活动类中初始化Text-To-Speech对象,然后使用string参数创建静态方法,该参数包含Text-To-Speech的语音方法。
And call this static method from your broadcastreceiver with the string you want to announce. 并使用您要声明的字符串从您的broadcastreceiver调用此静态方法。
public static void speakOut(String message) {
System.out.println("Message is: " + message);
tts.speak(message, TextToSpeech.QUEUE_ADD, null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.