Atlas91
Atlas91

Reputation: 5904

TextToSpeech interact dynamically

I'm using TextToSpeech to send an sms to someone in my contacts. I use a very ugly way to do it because i need say the all expression "send message to contact text message is hello" in one way. It would be better something like "Send message" and the app ask me to who and go on in this way.. So i can interact with the application like google now in dinamically way. The code i use so far is this one:

if(resultList.getText().toString().toLowerCase().contains(getResources().getString(R.string.messaggio))) //invio sms
                {

                    splitMessage = message.split(getResources().getString(R.string.destinatario), 2);
                                if(splitMessage.length>1){

                                     if((splitMessage[0].trim()).equalsIgnoreCase(getResources().getString(R.string.inviamessaggio)))
                                    {

                                        splitMessage[0] = "message";
                                        splitMessage[1] = splitMessage[1].trim();
                                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);


                                        splitMessageWithNameAndBody = splitMessage[1].split(getResources().getString(R.string.testomessaggio), 2);
                                        if(splitMessageWithNameAndBody.length>1)
                                        {
                                            splitMessage[0] = "text message";
                                        while (phones.moveToNext())
                                        {
                                            String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                                            String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                            splitMessage[1] = splitMessageWithNameAndBody[0].trim();
                                            splitMessageWithNameAndBody[1] = splitMessageWithNameAndBody[1].trim();
                                            if(Name.compareToIgnoreCase(splitMessage[1])== 0)
                                            {
                                                nameMatchFound = true;
                                                flag=1;
                                                String url = "sms:"+Number;
                                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                                intent.putExtra("sms_body", splitMessageWithNameAndBody[1]);
                                                startActivity(intent);
                                            }
                                        }


                                        if(!nameMatchFound)
                                        {
                                            Toast.makeText(getApplicationContext(), "Contatto non trovato o ci sono più corrispondenze", Toast.LENGTH_SHORT).show();
                                            DialogFragment df=new DialogTrial();
                                            df.show(getSupportFragmentManager(), "MyDialog"); 

                                        }
                                    } else {
                                        //Toast.makeText(getApplicationContext(), "Please say contact name followed by call or message keyword", Toast.LENGTH_LONG).show();
                                        while (phones.moveToNext()){
                                            String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                                            String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                            if(Name.compareToIgnoreCase(splitMessage[1]) == 0){
                                                nameMatchFound = true;

                                                String url = "sms:"+Number;
                                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                                intent.putExtra("sms_body", splitMessageWithNameAndBody[1]);
                                                startActivity(intent);
                                            }
                                        }
                                    }
                                }

                                else
                                {
                                    Toast.makeText(getApplicationContext(), "Please say contact name followed by call keyword", Toast.LENGTH_LONG).show();
                                }
                            }
                        //break;

        }

if it doesn't find any corrispondance it ask me to search in my contacts the right name. By the way, is what i want do possible? Thanks

Upvotes: 0

Views: 69

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93559

Is it possible? Sure. I have an app I wrote that does something similar- it reads out a text, asks if you want to respond, then asks for the response (if yes), then reads it and asks for confirmation before sending. Besically you need to make a state machine so you know how to process a response when it comes in and what to speak out for the next input.

Upvotes: 1

Related Questions