How to open maps and calls using ACTION_RECOGNIZE_SPEECH - android

I am developing a code in which the app takes input as speech and does the specific tasks.
I have got the code from changing speech to text but I am not able to put a if condition to create calls or open maps
here is a piece of code after getting text
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Text.setText(text.get(0));
String check = text.get(0);
if(check.equals("call")){
System.out.println("yes calling");
}
}
break;
}
Here I am performing if condition if he says call it should print "yes calling" but its not printing. what should I do?

Okay you can do it.
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Text.setText(text.get(0));
String check = text.get(0);
if(check.equals("call")){
Intent intent = new Intent(package name for app);
try{
startActivity(intent);
}catch(ActivityNotFoundException e){
}
}
}
break;
}

Related

Cannot use long inputs in Android Speech Recognition, error while initializing?

I'm trying to use Google Speech Recognition inside my application (a launcher). When I try to use it for short commands like Call to X or say a short number like 123456789 everything works fine, but as soon as I try to give a longer input (which requires more time to say) the Speech Recognition Activity Hangs on screen and it doesn't do anything.
I couldn't find anything useful in the Logs and I don't know if I'm making something wrong when initializing the Speech Recognition.
Here's when I initialize it:
var intent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
intent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
StartActivityForResult(intent, SpeechResult);
And here's where I use the result
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == SpeechResult && data != null)
{
var text = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
getterForNumber = text[0];
getterForNumber = getterForNumber.Replace(" ", String.Empty);
bool inInt = getterForNumber.All(char.IsDigit);
if (!inInt)
{
var activity2 = new Intent(this, typeof(fittizioAct));
activity2.PutExtra("fittizioData", "Cannot Recognise the Number");
activity2.PutExtra("direzione", "vocale");
StartActivity(activity2);
Finish();
}
else
{
var passNameAndNumberRubr = new Intent(this, typeof(VocalSavedAct));
passNameAndNumberRubr.PutExtra("saveNumberRubr", getterForNumber);
passNameAndNumberRubr.PutExtra("saveNameRubr", getterForName);
//sqldbRubrica.AddRecord(getterForName, getterForNumber);
StartActivity(passNameAndNumberRubr);
Finish();
}
}
else if (requestCode == SpeechResult && data == null)
{
AudioManager vol = (AudioManager)this.GetSystemService(Context.AudioService);
int volume = vol.GetStreamVolume(Android.Media.Stream.Music);
CrossTextToSpeech.Current.Speak("Voice Command Canceled",
pitch: 1,
speakRate: speed,
volume: (float)volume,
crossLocale: locale);
}
}
P.S. The code is in C# because I'm using Xamarin.
Try modifying the initent like this
var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, "Speak now");
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);

OK Google with string input? - Android

Is there a way to send a string to google now and get a response?
Example:
send "What time is it?" and get a string or audio response from google that says "It is 4:32pm."
String question = "What time is it?";
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(question);
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String converted_text = result.get(0);
}
break;
}
}
}
You should analyze the question by your self and get the answer and send it back using the TextToSpeech. For your example : You analyzed the string you find "Time" and you get the time from Android system and you send back the time as string to TextToSpeech.

Getting payment configuration from PayPal lib

I'm using PayPal in my monodroid application. I used the libraray that has been shared in this topic: http://forums.xamarin.com/discussion/comment/15331/#Comment_15331
But i have a problem at getting PaymentConfirmation object at OnActivityResult method. This is my code:
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
if (resultCode == Result.Ok ) {
var confirm = data.GetParcelableExtra ("com.paypal.android.sdk.paymentConfirmation") ;
PaymentConfirmation pc = (PaymentConfirmation)confirm;
if (confirm != null) {
try {
//Log.Info ("paymentExample", confirm.ToJSONObject ().ToString (4));
// TODO: send 'confirm' to your server for verification.
// see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
// for more details.\
} catch (JSONException e) {
Log.Error ("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
}else if (resultCode == Result.Canceled ) {
Log.Info ("paymentExample", "The user canceled.");
}else {
Log.Info ("paymentExample", "An invalid payment was submitted. Please see the docs.");
}
}
at the third line of the method. Compiler can not cast confirm to PaymentConfirmation. Do I need any other classes or codes in order to use data.GetParcelableExtra?
As written in a comment JavaCast should be used in this case. I think this happens when the binding generated for the type, does not contain a GetType method. Hence .NET does not know what to cast it to.
So your line:
PaymentConfirmation pc = (PaymentConfirmation)confirm;
will need to be:
PaymentConfirmation pc = confirm.JavaCast<PaymentConfirmation>();

Prompt voice recognition from within onActivityResult in Android

I hope I'll be clear enough.
In my app, I have set it so that pressing the back button activates google's voice recognition service. In the onActivityResult, it checks whether the first word is "call" or "text" and proceed accordingly with the rest of the spoken sentence. For text specifically, it goes like "text [contact name] message [message content]" and then sends it using an smsManager. But since it might get the name or the message wrong, I want it to read out the message and the person's name first for confirmation which I did just fine.
The problem is, to confirm or cancel, I want to also use voice recognition. If after the message is read out, the user says something like send and only then it should proceed to send the message. So, what I need to know is how/if can I implement this (newVoiceCommand) in the code below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
s="";
text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
decide = text.get(0).split("\\s");
for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
if (decide [0].equals("text")){
if (!s.equals("")){
msg = s.split(" message");
char[] stringArray = msg[0].toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
msg[0] = new String(stringArray);
contact = get_Number (test(msg[0]));
Intent intent = getIntent();
finish();
startActivity(intent);
String temp = "Are you sure you want to send " + msg[1] + " to " + test(msg[0]);
speakOut (temp);
if (newVoiceCommand.equals("send")){
try {
SmsManager smsManager = SmsManager.getDefault();
out.append(contact);
smsManager.sendTextMessage(contact, null, msg[1], null, null);
Toast.makeText(getApplicationContext(), "SMS Sent! to "+msg[0] + " at " +contact,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
else if (newVoiceCommand.equals("no")) out.append("Not sending");
}
}
else if (decide[0].equals("call")){
out.append (s);
if (!s.equals("")) {
call (s);
}
}
}
break;
}
}
}
My current attempt:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
s="";
//Grab the speech results and save them in an arraylist
text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Split each word into an array item
decide = text.get(0).split("\\s");
//Re-concatenate the words starting with the second word together
for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
//If the first word is "text", then send an SMS using the rest of the information spoken
if (decide [0].equals("text")){
if (!s.equals("")){
check (s);
Intent spIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
spIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_US");
try {
startActivityForResult(spIntent, RESULT_SPEECH2);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
switch (requestCode) {
case RESULT_SPEECH2: {
if (resultCode == RESULT_OK && null != data) {
text2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
s2 = "";
if (text2.get(0).equals("send")){
smsText (s);
}
else if (text2.get(0).equals("cancel")) return;
}
}
In short, all of the voice recognition results are going to come to you through an asynchronous call to onActivityResult. You don't block and wait for the result, so you need a way to determine whether the recognition is in response to a command or a confirmation at the time you receive the result.
This is what the requestCode parameter of Activity.startActivityForResult() is for:
Sometimes you want to get a result back from an activity when it ends.
For example, you may start an activity that lets the user pick a
person in a list of contacts; when it ends, it returns the person that
was selected. To do this, you call the startActivityForResult(Intent,
int) version with a second integer parameter identifying the call. The
result will come back through your onActivityResult(int, int, Intent)
method.
If you use two different values for requestCode (e.g., define private final static int REQUEST_SPEECH_COMMAND = 1, REQUEST_SPEECH_CONFIRMATION = 2) when you start the voice recognition activity, you can respond differently in onActivityResult. Note that you'll need to store the results of the first voice recognition (the action, message, and recipient) so this information will be available the second time around.

getting scan result when using zxing?

I am currently using the Zxing library in my app. After scanning the bar code of a book for example, how do I get things like the image, description, etc. from the scan result.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == RESULT_OK) {
IntentResult scanResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
} else if (resultCode == RESULT_CANCELED) {
showDialog("failed", "You messed up");
}
}
}
Thanks for your help
Zxing scans a variety of barcodes/QR codes, so the first thing you need to do is figure out if its a product UPC or a QR code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String response = data.getAction();
if(Pattern.matches("[0-9]{1,13}", response)) {
// response is a UPC code, fetch product meta data
// using Google Products API, Best Buy Remix, etc.
} else {
// QR code - phone #, url, location, email, etc.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(response));
startActivity(intent);
}
}
}
There are a number of web services available that will return product meta data given a UPC code. A fairly comprehensive one would be Google's Search API for Shopping. For example, you can get a json representations of the product with UPC = 037988482481 with an URL that looks like this:
https://www.googleapis.com/shopping/search/v1/public/products?country=US&key=your_key_here&restrictBy=gtin:037988482481
You'll need to replace "your_key_here" with your Google API key.
Best Buy also offers a RESTful products API for all of the products they carry which is searchable by UPC code.
You'll want to use an AsyncTask to fetch the product metadata once you have the UPC.
You dont need the 'IntentResult' or 'IntentIntegrator' for that.
You can do this:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
And then in onActivityResult:
if (requestCode == 0) {
if ((resultCode == RESULT_CANCELED) || (resultCode == -1000)) {
Toast.makeText(WebViewActivity.this, "Nothing captured",
Toast.LENGTH_SHORT).show();
} else {
String capturedQrValue = data.getStringExtra("barcode_data");
}
}
With this you scan the barcode, now there is in the Zxing code another library that uses Google API for looking up that ISBN.
In the class Intents.java you have the info of what extras the intent needs and the class ISBNResultHandler shows what is the result.
Hope it helps someone in the future.
You can check what the Android ZXing app does. The source for the Android client is in: ZXing Android client source code. For ISBN numbers, the source code for handling that is: Android ZXing app's ISBN Result Handler code
For product and book search, the Android code invokes these two functions from ResultHandler.java:
// Uses the mobile-specific version of Product Search, which is formatted for small screens.
final void openProductSearch(String upc) {
Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
"/m/products?q=" + upc + "&source=zxing");
launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}
final void openBookSearch(String isbn) {
Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
"/books?vid=isbn" + isbn);
launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}
In your onActivityResult do like following code
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
String qrCode=result.getContents();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
You haven't called result.getContents().
I just want to add that if you want to be able to press back without a RuntimeException surround your if statement with a try catch block. so:
try{
/// get scanned code here
} catch(RuntimeException e) {
e.getStackTrace();
{
Hope that helped the inevitable crash you would face without it.

Categories

Resources