简体   繁体   English

如何在从广播接收器调用的Activity中创建TextToSpeech对象?

[英]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.

相关问题 如何创建广播接收器? - How can i create a broadcast receiver? 如何在不重新启动的情况下将广播接收器的信息发送回我的主要活动 - how can i send information from broadcast receiver back to my main activity without restarting it 如何验证来自不同应用程序的广播接收器的 onReceive 方法是否被实际调用? - How can I verify that the onReceive method of a Broadcast Receiver from a different App is actually called? 如何将数据从活动中的功能传递到通知接收器广播接收器? - How to pass data from function in activity to a Notifcation Receiver Broadcast Receiver? 从广播接收器关闭活动 - Close the activity from a broadcast receiver 来自广播接收器的呼叫活动 - Calling activity from Broadcast receiver 我怎么知道何时从广播接收器与Android调用我的服务? - How do I know when my service is called from a broadcast receiver vs Android? 如何将参数从调用服务的活动传递到广播接收器 - How to pass argument to broadcast receiver from an activity that call a service 如何从另一个活动中调用TextToSpeech活动? - How to call TextToSpeech activity from another activity? 从活动中访问应用程序广播接收器? - Access an application broadcast receiver from within an activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM