IntentReceiver部件不允许结合服务 [英] IntentReceiver components are not allowed to bind to services

查看:158
本文介绍了IntentReceiver部件不允许结合服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.speech.tts.TextToSpeech;
进口android.telephony.SmsMessage;
进口android.util.Log;

公共类SmsBroadcastReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        [对象] rawMsgs =(对象[])intent.getExtras()获得(的PDU)。

        对于(对象原料:rawMsgs){
            SmsMessage消息= SmsMessage.createFromPdu((byte []的)生);

            Log.v([SMS]:,message.getMessageBody());

            TextToSpeech TTS =新TextToSpeech(背景下,NULL);

        }
    }
}
 

我得到一个错误,当我尝试初始化 TextToSpeech 。很显然,我不能绑定到服务于的BroadcastReceiver 时。有什么解决方法吗?

解决方案
  

有什么解决方法吗?

您有多大,比这更大的问题。

首先,的onReceive()被称为主应用程序线程上,在前台优先级。你可以不花超过几毫秒出现,而不会造成对前台应用程序的问题,如造成口吃的一场比赛。

二,你不知道该用户正在做时,短信进来的时候,他们可以玩游戏。它们可以是在电话呼叫在其扬声器的中间。你不能,不能不能决定单方面,你会通过文本到语音玩一个人的短信。你可以很容易被起诉,甚至更为糟糕。

我劝你现在停止编码,并坐下来思考 - 真的认为 - 它是什么,你正在尝试做的,和你的做法的后果

Here is my code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] rawMsgs = (Object[])intent.getExtras().get("pdus");

        for (Object raw : rawMsgs) {
            SmsMessage message  = SmsMessage.createFromPdu((byte[])raw);

            Log.v("[SMS]:", message.getMessageBody());

            TextToSpeech tts = new TextToSpeech(context, null);

        }
    }
}

I get an error when I try to initialize TextToSpeech. Apparently, I can't bind to services when in a BroadcastReceiver. Is there any workaround for this?

解决方案

Is there any workaround for this?

You have much, much bigger problems than that.

First, onReceive() is called on the main application thread, at foreground priority. You cannot spend more than a few milliseconds there without causing problems for a foreground application, such as causing stutter in a game.

Second, you do not know what the user is doing at the time of when an SMS message comes in. They may be playing a game. They may be in the middle of a phone call on their speakerphone. You cannot, cannot, CANNOT decide unilaterally that you are going to play a person's SMS via text-to-speech. You could easily be sued, and perhaps worse.

I urge you to stop coding now, and sit back and think -- really think -- about what it is that you are trying to do, and the ramifications of your approach.

这篇关于IntentReceiver部件不允许结合服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆