Android 在 BroadcastReceiver 中调用 TTS
我需要在 BroadcastReceiver 的子类中调用 TTS 服务。当我从 OnInitListener 实现该类时,它给出了运行时错误。
有没有其他方法可以在 BroadcastReceiver 中实现 TTS?
谢谢,
抱歉代码:
public class TextApp extends BroadcastReceiver implements OnInitListener {
private TextToSpeech tts;
private String message = "Hello";
@Override
public void onReceive(Context context, Intent intent) {
tts = new TextToSpeech(context, this);
message = "Hello TTS";
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
{
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
I need to call TTS service within subclass of BroadcastReceiver. When I am implement that class from OnInitListener, it gave run-time error.
Is there any other-way to implement TTS within BroadcastReceiver?
Thank You,
Sorry Code:
public class TextApp extends BroadcastReceiver implements OnInitListener {
private TextToSpeech tts;
private String message = "Hello";
@Override
public void onReceive(Context context, Intent intent) {
tts = new TextToSpeech(context, this);
message = "Hello TTS";
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
{
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码不起作用:
BroadcastReceiver 上的上下文是“受限上下文”。这意味着您无法在 BroadcastReceiver 中的上下文中启动服务。因为 TTS 是一项服务,所以它不调用任何东西。
最好的解决方案是您通过调用该服务的活动在 BroadcastReceiver 上启动另一个意图。
然后在活动中使用 extras 中的参数调用 TTS 服务
Your code didn't work on :
Context on BroadcastReceiver is a "restricted context". It means you cannot start service on context in BroadcastReceiver. Because TTS is a service, so it doesn't call anyting.
The Best Solutions is you start another intent on BroadcastReceiver with activity that call the service.
And then on the activity you call the TTS service with parameter from extras
您可以尝试使用 JobIntentService (Android-O 后)或 IntentService 从广播接收器调用 TTS。与为 TTS 提供正确上下文而启动活动相比,它的开销更少。请注意,您无法将广播接收器的上下文提供给 TTS。
这是我的代码片段,我使用 JobIntentService 实现了同样的效果。
在您的自定义广播接收器的 onReceive() 内部调用您的自定义 JobIntentService,如下所示:
MySpeakService.java 是这样的:
You can try using either JobIntentService (Post Android-O) or IntentService to invoke TTS from Broadcast receiver. It has less overhead than launching an activity for the sake of giving TTS a correct context. Note that you cannot give a broadcast receiver's context to TTS.
Here is my code snippet where I acheived same thing using JobIntentService.
Inside your custom Broadcast receiver's onReceive() invoke your custom JobIntentService like this:
And MySpeakService.java is this:
Al Zil 的回答并不完全正确。 Android TTS 是一项有界服务。广播接收器确实具有有限的上下文,但它们无法将自己绑定到任何服务。但是,他们可以启动服务。从 Activity 启动 tts 是可以的,但如果您不需要 UI,您也可以从服务初始化它。看看这个答案看看它是如何完成的
祝你好运。
Al Zil answer is not totally correct. Android TTS is a bounded service. Broadcast receivers truly has a limited context but they can't bind themselves to any service. However, they can START a service. Starting the tts from activity is ok, but if you don't need UI you can also initialize it from a service. Look at this answer to see how it's done
Good luck.