Google Voice Recognition Android onActivityResult - java

In my google voice recognition, I'm trying to add commands. The commands should be in this piece of code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
// add commands
}
}
I am guessing it would be a else if statement but I'm confused on how to start it.
if() {
// put code here
}
But what should I put in the if and the put code here?

You should check the requestCode and resultCode like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SystemData.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// TODO something with matches
}
super.onActivityResult(requestCode, resultCode, data);
}
The 'matches' ArrayList will contain all the recognized words in order.

Related

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

Why ThumbnailUtils.createVideoThumbnail() return null when get video from gallery

I have a video from gallery and I need get its thumbnail. I use ThumbnailUtils class for this purpose, but it returns null instead of expected Bitmap.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==GalleryPick && resultCode==RESULT_OK && data!=null)
{
Uri resultUri = data.getData();
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(resultUri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);
Glide.with(this).load(thumb).into(videoView);
}
}
I also try below code but it doesn't work
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==GalleryPick && resultCode==RESULT_OK && data!=null)
{
Uri resultUri = data.getData();
File file = new File(resultUri.getPath()); //
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND); //
Glide.with(this).load(thumb).into(videoView);
}
}

How to connect 2 onActivityResult?

I have problem with adding a Facebook Authentication because I'm using Google Authentication too and this is why I have error
public class SignUpActivity extends AppCompatActivity {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
//Google
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
//and Facebook
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
How you can see I have two onActivityResult methods. Is there any way to connect them and get rid of an error ?
This is how looks like my error
method onActivityResult(int,int,Intent) is already defined in class
SignUpActivity
It's just a communicate of existing two the same methods.
Thanks.
onActivityResult is a Android method, it receives results from activities you started with startActivityForResult and returns the request_code int you provided.
The solution is to use different REQUEST_CODES at startActivityForResult, so you can compare then in onActivityResult
like:
private static final int FACEBOOK_REQUEST_CODE = 1;
private static final int GOOGLE_REQUEST_CODE = 0;
startActivityForResult(googleLoginIntent, GOOGLE_REQUEST_CODE)
startActivityForResult(facebookLoginIntent, FACEBOOK_REQUEST_CODE)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_REQUEST_CODE) {
//do the code for google result
} else if (requestCode == FACEBOOK_REQUEST_CODE) {
// do the code for facebook result
}
}

How to access bundle that AccountManager.newChooseAccountIntent returns?

I am using the intent newChooseAccountIntent to have a user pick an account. I read here that the intent returns a bundle with the keys KEY_ACCOUNT_NAME for account name and KEY_ACCOUNT_TYPE for account type.
https://developer.android.com/reference/android/accounts/AccountManager.html
My question is how do I access the bundle the intent returns? Would I do data.getBundleExtra() in my activity result function?
Activity result function code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Log.d("frag", "is return empty " + data.getBundleExtra("KEY_ACCOUNT_NAME"));
}
else if (resultCode == RESULT_CANCELED) {
Log.d("frag", "intent fired and something went wrong");
}
}
As per the Javadoc, both AccountManager.KEY_ACCOUNT_NAME and AccountManager.KEY_ACCOUNT_TYPE are strings, meaning you use getStringExtra():
if(resultCode == RESULT_OK) {
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
Log.d("frag", "Got " + name + ", " + type);
}

Receiving result in android

I am beginner to android..I Integrated qr scanner in my app..problem is I added qr scanner button in two fragment..for receiving result I added on activityResult method..In my mainactivity..Is any another method for receiving result in android Instead of OnactivityResult method..If not their I should I add multiple onActivityResult method In my Mainactivty..can anyOne help me..
below is the my code for onActivityresult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
if (scanResult != null) {
if (scanResult.getContents() == null) {
Log.d("ScanFragment", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
//result fragment
super.onActivityResult(requestCode, resultCode, data);
}
// }
}
static final int FRAGMENT_ONE_REQUEST = 1; // The request code
static final int FRAGMENT_TWO_REQUEST = 2; // The request code
then use this from fragment one
startActivityForResult(intent, FRAGMENT_ONE_REQUEST);
then use this from fragment two
startActivityForResult(intent, FRAGMENT_TWO_REQUEST);
in each fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
//some code
if (requestCode == FRAGMENT_ONE_REQUEST) {
}
}
}

Categories