I am currently working on an app that takes user input by voice command. The program recognizes the words using the google api. I want to be able to compare the user voice input to hard coded strings. My problem is that I do not know how to code this function. I am using if else statements to match the hard coded string, but the Toast is always the wrong one. Could anyone please guide me? Thank you for your time!
/**
* Receiving speech input
* */
#Override
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);
txtSpeechInput.setText(result.get(0));
if(txtSpeechInput.equals("weather")) {
Toast.makeText(getApplicationContext(), "Good", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "It's a difficult question... I'm sorry", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
}
You are comparing your String to a TextView object instead of directly to the data.
Try if( result.get(0).equals("weather") ) { ... }
Or at least if( txtSpeechInput.getText().toString().equals("weather") ) { ... }
Related
I am building an application using android speech recognition intent. I have build the application, now I want to send the recognized outputs to local server so that I can view the output of the recognizer from any device connected to the same network. Below is my code:
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);
mTextFromSpeech.setText(result.get(0));
String finalResult = result.get(0);
System.out.println("Detected Command Is: " + finalResult);
}
break;
}
}
}
I want to send the result from finalresult on local server, please help.
I had just now created a new API key after enabling Places API in the developer console.
Code for implementing Place picker API:
Places.initialize(getApplicationContext(), "#string/google_place_api");
List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596));
Intent autocompleteIntent =
new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
.setLocationBias(bounds)
.setTypeFilter(typeFilters.get(0))
.build(getApplicationContext());
startActivityForResult(autocompleteIntent, 1001);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getApplicationContext(),place.getName()+ ", " + place.getId(), Toast.LENGTH_SHORT).show();
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.getStatusMessage(), Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Please select a location", Toast.LENGTH_SHORT).show();
}
}
}
The problem is that I keep getting a toast showing The provided API key is invalid whenever I try to search for a location. How is this possible? The API key I got is completely new.
Edit: This is the error that I keep getting in my Logcat
The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-04-29 19:36:10.285 14975-14975/com.example.myapplication E/Places: Error while autocompleting: REQUEST_DENIED
You are incorrectly passing the API Key.
Replace this line:
Places.initialize(getApplicationContext(), "#string/google_place_api");
with:
Places.initialize(getApplicationContext(), getString(R.string.google_place_api));
I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.
I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.
The first way I coded it was to check for the existence of the intent extra:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data.getExtras().containsKey("extra1")) {
extra1 = data.getStringExtra("extra1");
}
if (data.getExtras().containsKey("extra2")) {
extra2 = data.getStringExtra("extra2");
}
}
// plus rest of code for checking for RESULT_CANCELED
}
}
The other way I coded it is by using custom result codes. The result codes RESULT_OK_CATEGORY and RESULT_OK_SEARCH are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through setResult(MainActivity.RESULT_OK_CATEGORY, intent) or setResult(MainActivity.RESULT_OK_SEARCH, intent) respectively.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK_CATEGORY) {
extra1 = data.getStringExtra("extra1");
} else if (resultCode == RESULT_OK_SEARCH) {
extra2 = data.getStringExtra("extra2");
}
// plus rest of code for checking for RESULT_CANCELED
}
}
Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?
You should use resultCode because it's completely under your control. You may lose values set in Intent's extras, as revealed in this question (referenced by Ajay in the comments).
Note, if you use many custom result codes, it has been recommended to use a switch statement for code clarity.
this is my first question on stack*overflow*!
I am using Eclipse with Android Development Tools. I have copied the code from GitHub - ZXing to scan a QR code using Barcode Scanner (many of you have seen this code before, I'm sure):
public void onClick(View v)
{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent,0);
}
I am able to get the Barcode Scanner started and it returns results in the onActivityResult() method.
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
contents = intent.getStringExtra("SCAN_RESULT");
format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
if(contents == "1")
{
contentsLat = contents;
}
else if(contents == "0")
{
contentsRow = contents;
}
else
{
Toast.makeText(getApplicationContext(), "Code Not Recognized\n"+contents+"\n1", Toast.LENGTH_LONG).show();
}
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Toast.makeText(getApplicationContext(), "Unsuccessful Scan", Toast.LENGTH_SHORT).show();
}
}
}
However, the else statement is always executed...unless I explicitly equate contents to 1. Below is a link to the screenshot (I don't have embedded image privileges yet) of the else body being executed and the toast clearly indicates that contents=1. What have I done wrong? Did I miss something?
Thanks everybody.
Welcome to stackoverflow!
I have seen this is a very common issue in new Java programmmers :), for String comparison we must use the equals() method.
Change your code to:
// Handle successful scan
if(contents.equals("1")){
contentsLat = contents;
}
else if(contents.equals("0")){
contentsRow = contents;
}
More info:
Java comparison with == of two strings is false?
The equals() Method
I'm having trouble getting zxing to do anything after scanning a barcode. I call the zxing using:
IntentIntegrator.initiateScan(MainActivity.this);
And my onActivityResult looks like this
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
if (format == "PRODUCT_CODE"){
//If the barcode scanned is of the correct type then pass the barcode into the search method to get the product details
Toast.makeText(getApplicationContext(),"You scanned " + contents, 3).show();
}
else{
//If the barcode is not of the correct type then display a notification
Toast.makeText(getApplicationContext(),"You didn't scan a product code", 3).show();
}
} else if (resultCode == RESULT_CANCELED) {
//If the scan is cancelled then display a notification
Toast.makeText(getApplicationContext(),"You cancelled the input", 3).show();
}
}
}
But whenever I exit zxing nothing is displayed. I tried using the example in the zxing wiki
but whenever I try to replace yourActivity with MainActivity or MainActivity.this I receive errors (I get told MainActivity cannot be resolved to a string and with MainActivity.this that The constructor IntentIntegrator(MainActivity) is undefined).
Basically I have no idea what I'm doing and why it's not working. Any help would be greatly appreciated.
The problem is exactly the same as in your other question. You can't compare strings with ==, but must use equals().