Android TextToSpeech使用WakefulBroadcastReceiver

Android TextToSpeech使用WakefulBroadcastReceiver,android,broadcastreceiver,text-to-speech,Android,Broadcastreceiver,Text To Speech,如何使用WakefulBroadcastReceiver使用文本到语音功能onPause方法我为此制作了以下类: 我正在使用Receiver接收GCM推送通知,此代码在Receive方法上运行良好,但当应用程序处于暂停状态且NotificationManager中的通知在此时应用程序崩溃时,请帮助我解决此问题 GCMBroadcastereceiver 演讲者 以下是我所面临的例外 Caused by: android.content.ReceiverCallNotAllowedExceptio

如何使用WakefulBroadcastReceiver使用文本到语音功能onPause方法我为此制作了以下类: 我正在使用Receiver接收GCM推送通知,此代码在Receive方法上运行良好,但当应用程序处于暂停状态且NotificationManager中的通知在此时应用程序崩溃时,请帮助我解决此问题 GCMBroadcastereceiver

演讲者

以下是我所面临的例外

Caused by: android.content.ReceiverCallNotAllowedException:
BroadcastReceiver components are not allowed to bind to services
at android.app.ReceiverRestrictedContext.bindService(ContextImpl.java:173)
at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:627)
at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:597)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
at sss.sss.sss.Speaker.<init>(Speaker.java:20)
at sss.sss.sss.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:47)

您应该在onReceive中启动服务,并将消息传递给speak

@Override
public void onReceive(Context context, Intent intent)
{
     intent.setAction("speak");
     intent.putExtra("welcome_message", "asdas");
     intent.putExtra("body", "asdas");
     startService(intent.setComponent(new ComponentName(context.getPackageName(),
                MyService.class.getName())));
      // Where MyService is the service that you implements TTS
}
然后在服务班

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    if (intent != null)
    {
        String action = intent.getAction();
        if ("speak".equals(action))
        {
             // check if TTS is initialize if so speak the extra
             // otherwise save the extra to class members and when onInit is called
             // check if these class members are null then speak 
        }
    }
}
@Override
public void onReceive(Context context, Intent intent)
{
     intent.setAction("speak");
     intent.putExtra("welcome_message", "asdas");
     intent.putExtra("body", "asdas");
     startService(intent.setComponent(new ComponentName(context.getPackageName(),
                MyService.class.getName())));
      // Where MyService is the service that you implements TTS
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    if (intent != null)
    {
        String action = intent.getAction();
        if ("speak".equals(action))
        {
             // check if TTS is initialize if so speak the extra
             // otherwise save the extra to class members and when onInit is called
             // check if these class members are null then speak 
        }
    }
}