I have several onActivityResult activities in my Android app and I am having an issue with two activities running at one time. When I open up my ZXing barcode scanner it will close the connection to my Bluetooth printer. Here is my code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
What would be the best way to split up these activities so that they do not overlap each other?
UPDATE:
As #Mahfa stated I need to add a requestCode to the scanning activity. This is my code for the button that starts the scanning activity:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
What do I need to do to add a requestCode to this?
#Mahfa put me on the right track with the links that he posted and eventually I found this code example that cleared up my issue http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java
Here is my completed code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
case IntentIntegrator.REQUEST_CODE:
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
break;
}
I did not have to change anything on my OnClickListener. Thanks for pointing me in the right direction #Mahfa.
Related
I am recording/selecting a video in my RecordVideo.java activity. Once the video is recorded/selected the I press the upload button and go to the PostActivity.java file. But once I'm in there for some reason onActivityResult isn't being called. I tried to toast and Log.d for some type of error or info but nothing came up.
I checked the answer on here to the same problem I'm having but none of them worked for me.
Can someone please help ?
RecordVideo.java:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == REQUEST_CODE_VIDEO_CAPTURE && resultCode == RESULT_OK && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
} else if (requestCode == 5 && resultCode == RESULT_OK && data != null && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void uploadUserVideo(Uri videoUri) {
if (videoUri != null) {
Intent intent = new Intent(RecordVideo.this, PostActivity.class);
intent.putExtra("videoUri", videoUri.toString());
startActivity(intent);
finish();
} else {
Toast.makeText(this, "It's null", Toast.LENGTH_SHORT).show();
}
}
PostActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
Bundle extras = getIntent().getExtras();
if (extras != null) {
//videoUri = extras.getInt(videoUri);
videoUri = Uri.parse(extras.getString("videoUri"));
//Toast.makeText(this, "It's not null", Toast.LENGTH_SHORT).show();
} else {
// handle case
Toast.makeText(this, "Post null", Toast.LENGTH_SHORT).show();
}
videoView = findViewById(R.id.video_added);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
close = findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.d("TAG3", "I'm here." + requestCode);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
Toast.makeText(this, "No problem.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "A problem.", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
My App1 launch App2:
case R.id.launch_button: {
try {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.app1");
intent.putExtra("Price", getAmount());
intent.putExtra("Description", getProductId());
startActivityForResult(intent, PAYMENT_REQUEST);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "Activity not found" );
}
break;
}
When App2 finish processing should return RESULT_OK or RESULT_CANCELED:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult " + requestCode + " " + resultCode + " " + data);
if (requestCode == PAYMENT_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Payment success", Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Payment fail", Toast.LENGTH_LONG).show();
}
}
}
but RESULT_CANCELED appears immediately when the App2 launch.
Why am I receiving RESULT_CANCELED before even the App2 launch? What I'm doing wrong?
App2 return result code:
#Override
public void onPaymentSuccess() {
activity.setResult(Activity.RESULT_OK);
activity.finish();
}
#Override
public void onPaymentFail() {
activity.setResult(Activity.RESULT_CANCELED);
activity.finish();
}
I also dont receive any result when App2 finish..
Note: App2 use Fragment
I am using createConfirmDeviceCredentialIntent() for pin authentication in my android application. If I press back button from my pin authentication intent, the createConfirmDeviceCredentialIntent closes and it displays main activity. I want that from that intent if someone presses back button the app should get closed. `KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent i = keyguardManager.createConfirmDeviceCredentialIntent("unlock", "confirm_pattern");
try {
startActivityForResult(i, LOCK_REQUEST_CODE);
} catch (Exception e) {
Log.d("exception occured", e.toString());
}
} #Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case LOCK_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Intent a = new Intent(MainActivity.this, HomeActivity.class);
startActivity(a);
textView.setText("unlock_success");
} else {
textView.setText("unlock_failed");
}
break;
}
}`
I have created two edit texts. And i created two buttons. By clicking on a button, it will ask for user to speak and it will convert the speech to text and it will
be displayed in the edit text.
I have called the voice to text function two times. One for first edit text and other for second edit text. But it displaying the error.
Please help me to solve this problem.
Here is my code:
private void promptSpeechInput1() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* 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);
txtSpeechInput1.setText(result.get(0));
}
break;
}
}
}
//////////////
private void promptSpeechInput2() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* 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);
txtSpeechInput2.setText(result.get(0));
}
break;
}
}
}
And the error shows on the line:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
You are redefining the onActivityResult method. Use only one instance for your purpose
/**
* Receiving speech input and output
* */
#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);
txtSpeechInput1.setText(result.get(0));
}
break;
}
case REQ_CODE_SPEECH_OUTPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput2.setText(result.get(0));
}
break;
}
}
}
and modify this in your promptSpeechInput2 method. Use different request code i.e. not REQ_CODE_SPEECH_INPUT else use REQ_CODE_SPEECH_OUTPUT with different value
try {
startActivityForResult(intent, REQ_CODE_SPEECH_OUTPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
i want user Email id via AccountPicker.newChooseAccountIntent. i want Detect User Cancel AccountPicker dialog
here is a code
private static final int REQUEST_CODE_EMAIL = 1;
private TextView email = (TextView) findViewById(R.id.email);
try {
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
// TODO
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
}
}
There is also a RESULT_CANCELED or RESULT_CANCEL constant in an Activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
} else if(requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
}