Im sending intent to use speech recognizer
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US)
putExtra(RecognizerIntent.EXTRA_PROMPT, R.string.speech_prompt)
}
startForResult.launch(intent)
But how can I extract the results from it? I've tried several way
private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
Log.d("testing","resultData ${result.data}")//here I see HAS EXTRAS
result.data.let {
inputField.setText(it?.getStringArrayExtra(RecognizerIntent.EXTRA_RESULTS)?.get(0))
//I've used this one and it doesn't work as well
inputField.setText(it?.getStringArrayExtra(SpeechRecognizer.RESULTS_RECOGNITION)?.get(0))
}
}
}
So what is the correct way to extract the result ?
Here is the solution that works for me
private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
myInput.setText(result.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.first())
}
}
Related
Currently I am working on a project for my college, and i have discovered that onActivityResult is deprecated. What can be done to handle it?
This is my code that troubles me
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result = CropImage.getActivityResult(data)
if (resultCode == RESULT_OK) {
image = result.uri
Glide.with(this).load(image).into(binding.imgPick)
} else {
Snackbar.make(
binding.root,
"Image not selected",
Snackbar.LENGTH_SHORT
).show()
}
}
}
I tried to find a solution on stackoverflow and already tried to implement couple of thing but with no luck.
You can use the following way :
fun openActivityForResult() {
startForResult.launch(Intent(this, AnotherActivity::class.java))
}
val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent = result.data
// Handle the Intent
//do stuff here
}
}
You can use this in activity or fragment.
First define intent result launcher.
var picker: ActivityResultLauncher<Intent>
then
picker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK && it.data != null) {
//add your code here
}
}
And now launch your activity.
val intent = Intent(context, ImagePickerActivity::class.java)
picker.launch(intent)
Even after adding dependencies, Android Studio still can't find registerForActivityResult() method. How can I fix this problem?
These dependencies has been added:
implementation("androidx.activity:activity-ktx:1.5.0")
implementation("androidx.fragment:fragment-ktx:1.5.0")
implementation("androidx.appcompat:appcompat:1.4.2")
And I have written this code:
var someActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback<ActivityResult> { result ->
if (result.resultCode == Activity.RESULT_OK) {
//There are no request codes
val data = result.data
}
})
I'm creating file using
private val getContent = registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri ->
uri?.let { writeToFile(requireActivity(), it) }
}
and then launch it:
getContent.launch("file.csv")
so when i'm in launcher i can rename file before saving and also change file type which is not what i expect. So can i let user change name but hardcore this type?
private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{ result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
// make action
}
}
private fun createFile() {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/png"
intent.putExtra(Intent.EXTRA_TITLE, "AA.png")
startForResult.launch(intent)
}
What is the alternative to get callback for startUpdateFlowForResult in InAppUpdates instead of onActivityResult since it is deprecated?
We have to wait for Google Play team to migrate away from the deprecated APIs. You can follow this issue on Google's Issue Tracker.
Create this result launcher
private val updateFlowResultLauncher =
registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult(),
) { result ->
if (result.resultCode == RESULT_OK) {
// Handle successful app update
}
}
After that try to launch your intent like this
val starter =
IntentSenderForResultStarter { intent, _, fillInIntent, flagsMask, flagsValues, _, _ ->
val request = IntentSenderRequest.Builder(intent)
.setFillInIntent(fillInIntent)
.setFlags(flagsValues, flagsMask)
.build()
updateFlowResultLauncher.launch(request)
}
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
starter,
requestCode,
)
Give it a try!
You can use below code snippet alternative of onActivityResult()
First Activity
Step 1
private val openActivity =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
handleActivityResult(REQUEST_CODE, it)
}
Step 2
openActivity.launch(
Intent(this, YourClass::class.java).apply {
putExtra(ANY_KEY, data) // If any data you want to pass
}
)
Step 3
private fun handleActivityResult(requestCode: Int, result: ActivityResult?) {
Timber.e("========***handleActivityResult==requestActivty==$requestCode====resultCode=========${result?.resultCode}")
if (requestCode == REQUEST_CODE) {
when (result?.resultCode) {
Activity.RESULT_OK -> {
val intent = result.data // received any data from another avtivity
}
Activity.RESULT_CANCELED->{
}
}
}
}
In second class
val intent = Intent()
intent.putExtra(ANY_KEY, data)
setResult(Activity.RESULT_OK, intent)
finish()
Getting an exception :
No Activity found to handle Intent { act=android.intent.action.VIEW dat=upi://pay?pa=xyx#idfcbank&pn=xyz&mc=5311&tr=txRefId&tn=txDesc&am=12.0&cu=INR pkg=com.google.android.apps.nbu.paisa.user }
Followed these steps for Gpay integration.
https://developers.google.com/pay/india/api/android/in-app-payments
fun gpay() {
val GOOGLE_PAY_PACKAGE_NAME = "com.google.android.apps.nbu.paisa.user"
try {
val uri = Uri.Builder()
.scheme(GooglePayMaker.SCHEME_UPI)
.authority(GooglePayMaker.AUTHORITY_PAY)
.appendQueryParameter(JSONConstants.PAYEE_VPA_KEY, JSONConstants.PAYEE_VPA_VALUE)
.appendQueryParameter(JSONConstants.PAYEE_NAME_KEY, JSONConstants.PAYEE_NAME_VALUE)
.appendQueryParameter(JSONConstants.PAYEE_MERCHANT_CODE_KEY, JSONConstants.PAYEE_MERCHANT_CODE_VALUE)
.appendQueryParameter(JSONConstants.PAYEE_TX_REF_ID_KEY, "xyx")
.appendQueryParameter(JSONConstants.PAYEE_TX_DESC_KEY, "xyx")
.appendQueryParameter(JSONConstants.PAYEE_TX_AMT_KEY, "12.0")
.appendQueryParameter(JSONConstants.PAYEE_CURR_KEY, JSONConstants.PAYEE_CURR_VALUE)
.build()
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
intent.setPackage(GOOGLE_PAY_PACKAGE_NAME)
startForResult.launch(intent)
} catch (exception: Exception) {
Log.d("TAG", "" + exception.message)
exception.printStackTrace()
}
}
val startForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent = result.data
}
}
FYI : using valid credentials.