Question

I am new to android.I am trying to create a caller name announcer app. Following is my code. The logs show that the code is executing just fine i.e. the TextToSpeech constructor is executing and reaching the onInit method till the very end, however, when I test it, the audio is not heard.

This is my 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

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?

Était-ce utile?

La solution

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.

And call this static method from your broadcastreceiver with the string you want to announce.

public static void speakOut(String message) {

    System.out.println("Message is: " + message);

    tts.speak(message, TextToSpeech.QUEUE_ADD, null);

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top