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
Related
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 have make payments through PayPal native android mobile sdk. Below is the success response
{"response":{"state":"approved","id":"PAY-someID","create_time":"2017-06-21T18:01:33Z","intent":"sale"},"client":{"platform":"Android","paypal_sdk_version":"2.15.3","product_name":"PayPal-Android-SDK","environment":"sandbox"},"response_type":"payment"}
But when back-end wants to verify with the payment id i.e PAY-someID, it is throwing error json like below every time
[debug] application - verifyPayment mitemreqCppverifpmtrequest(PAY-someID)
[debug] application - verifpmtrequest case mendpointt https://api.sandbox.paypal.com/v1/payments/payment/PAY-someID
[debug] application - tx.get.body{"name":"INVALID_RESOURCE_ID","message":"Requested resource ID was not found.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID","debug_id":"someID"}
Below is the android code
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
// The following are only used in PayPalFuturePaymentActivity.
.merchantName("Example Merchant")
.merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(SampleActivity.this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
return new PayPalPayment(new BigDecimal("0.01"), "USD", "sample item",
paymentIntent);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i(TAG, confirm.toJSONObject().toString(4));
}
}
}
The problem is similar to the below
https://github.com/paypal/PayPal-Android-SDK/issues/330
No idea how to solve this.
Try this one this is working code of my Google PlayStore application.
PayPalConfiguration config = new PayPalConfiguration().environment(PayPalConfiguration.ENVIRONMENT_SANDBOX).clientId("Your id");
Intent intent = new Intent(context, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
PayPalPayment payment = new PayPalPayment(new BigDecimal(charge), "USD", eventName, PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(activity, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, 1089);
Don't Forget to stop service in OnDestroy().
#Override
protected void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
Also stop service inside OnActivityResult When you perform all your task.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 1089) {
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null)
{
try
{
// Your code after successful response.
} catch (JSONException e) {
Log.error(getClass().getName(), e);
}
}
stopService(new Intent(this, PayPalService.class));
} else if (resultCode == Activity.RESULT_CANCELED) {
//
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
//
}
}
I am trying to get e-mail address of the people the user has invited from my app
This is my code:
private void onInviteClicked() {
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
.setCustomImage(Uri.parse("http://jennstrends.com/wp-content/uploads/2013/10/bad-profile-pic-2.jpeg"))
.setCallToActionText(getString(R.string.invitation_cta)).setCallToActionText("email").build();
startActivityForResult(intent, REQUEST_INVITE);
}
int REQUEST_INVITE = 10101;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
if (requestCode == REQUEST_INVITE) {
if (resultCode == RESULT_OK) {
// Get the invitation IDs of all sent messages
final String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
final List<String> members = new ArrayList();
members.add(GoogleAuthHelper.getInstance().getUser().getProjectId());
for (String id : ids) {
members.add(id);
}
ApiClient.getInstance(getBaseContext()).addMembers(members, new OnPostListener<String>() {
#Override
public void onError(ANError error) {
Log.e("Server", "Members: " + error.getErrorCode());
}
#Override
public void onResponse(String val) {
Log.d("Server", "Response from Member: " + val.toString());
}
});
} else {
// Sending failed or it was canceled, show failure message to
// the user
// ...
Log.d("Server", "Invitation Failed code: " + resultCode);
}
}
}
Now how do I get the email addresses of the list of people the user has invited. AppInviteInvitation.getInvitationIds() are not the email ids. Someone help me out! Please!
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.