This is my code, an attempt to launch an in app purchase when a button is pressed:
purchaseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(purchased.equals("remove_ads")) {
Toast.makeText(getApplicationContext(), "You already own the item.",
Toast.LENGTH_LONG).show();
}
else{
try{
Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(),
"remove_ads", "inapp", "key");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
try{
startIntentSenderForResult(pendingIntent.getIntentSender(), ///NPE here
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
catch(IntentSender.SendIntentException ee){
Toast.makeText(getApplicationContext(), "Error was: " + ee,
Toast.LENGTH_LONG).show();
}
catch(NullPointerException n){
Toast.makeText(getApplicationContext(), "Error was: " + n,
Toast.LENGTH_LONG).show();
}
}
catch (RemoteException e){
Toast.makeText(getApplicationContext(), "Error was: " + e,
Toast.LENGTH_LONG).show();
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(store.this, "remove_ads", 10001,
mPurchaseFinishedListener, "key");
}
}
}
});
and for some reason I get the NPE at the line indicated startIntentSenderForResult... and I don't understand what could cause it. Previously I used this code in another in app purchase exactly as shown except it was a different sku. Could it make a difference since I have 2 identical copies of this code block shown in the same class? Stacktrace doesn't show anything useful either, just the NPE.
Related
I have implemented Razor Pay payment gateway in my Android application. Everything is working fine for debit/credit card.
I am facing problem with UPI payment. Actually for UPI payment user has to visit UPI app to make the payment. Everything is working for UPI as well but the only problem is callback methods are not invoked if payment is successful or not until and unless I visit the app again.
This is big problem for me because sometime if user pay via UPI app and does not open the app again, it's been difficult for me to save the entries in the database for the payment.
I am saving entries in the database every time success callback method is invoked. How do I call success method when app is in background but not closed.
Here is the code snippet:
To open the payment activity:
Intent intent = new Intent(context, PaymentActivity.class);
intent.putExtra("orderId", order_id);
intent.putExtra("totalAmount", String.valueOf(totalPrice));
intent.putExtra("email", email);
intent.putExtra("phoneNo", phone_number);
intent.putExtra("userId", user_id);
startActivityForResult(intent, 2);
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
To call the Razor pay Activity:
final Activity activity = this;
price = Double.parseDouble(total_price);
double paisa_double = price * 100;
int paisa_int = (int) paisa_double;
final Checkout checkout = new Checkout();
checkout.setKeyID(getResources().getString(R.string.razor_pay_key));
try {
JSONObject options = new JSONObject();
options.put("name", "Razorpay Corp");
options.put("description", "Order No: " + order_id);
options.put("order_id", razor_id);
//You can omit the image option to fetch the image from dashboard
options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
options.put("currency", "INR");
options.put("amount", String.valueOf(paisa_int));
//options.put("amount", "100");
JSONObject preFill = new JSONObject();
preFill.put("email", email);
preFill.put("contact", phone_no);
options.put("prefill", preFill);
JSONObject notes = new JSONObject();
notes.put("notes", order_id);
options.put("notes", notes);
checkout.open(activity, options);
} catch (Exception e) {
Toast.makeText(activity, "Error in payment: " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
And these are the callback methods:
#Override
public void onPaymentSuccess(String razorpayPaymentID, PaymentData paymentData) {
try {
//Toast.makeText(this, "Payment Successful: " + razorpayPaymentID, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("razorpayPaymentID", razorpayPaymentID);
setResult(2, intent);
finish();
} catch (Exception e) {
Log.e(TAG, "Exception in onPaymentSuccess", e);
}
}
#Override
public void onPaymentError(int code, String response, PaymentData paymentData) {
try {
Toast.makeText(this, "Payment failed: " + code + " " + response, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(TAG, "Exception in onPaymentError", e);
}
}
Use Razor pay web hooks in the back end code for the above problem.
Link:
https://razorpay.com/docs/webhooks/
In android studio I am able to create a mediaRecorder instance and record audio, I can then create an instance of a mediaRecorder with a different audio source and record audio. The problem is that I cannot have two mediaRecorders at one time (or so I think).
In addition to mediaRecorder, I have looked into using two different AudioRecord objects but it appears someone here tried that about a month ago and it does not work either. I have looked into the mediaMuxer which may be the key to this, but I am new to the concept of multiplexing and do not know how to implement something of this kind.
// not to professional standards
btnRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkPermissionFromDevice()) {
pathsave = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/"
+ UUID.randomUUID().toString() +
"_audio_record.3gp";
setupMediaRecorder();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
btnStopRecord.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording...",
Toast.LENGTH_SHORT).show();
}
else{
requestPermission();
}
}
});
//------------------------------------------------------------------------
btnRecord2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkPermissionFromDevice()) {
pathsave2 = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/"
+ UUID.randomUUID().toString() +
"_audio_record.3gp";
setupMediaRecorder2();
try {
mediaRecorder2.prepare();
mediaRecorder2.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay2.setEnabled(false);
btnStop2.setEnabled(false);
btnStopRecord2.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording...",
Toast.LENGTH_SHORT).show();
}
else{
requestPermission();
}
}
});
//-----------------------------------------------------------------------
private void setupMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(pathsave);
}
//------------------------------------------------------------------------
private void setupMediaRecorder2() {
mediaRecorder2 = new MediaRecorder();
mediaRecorder2.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder2.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder2.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder2.setOutputFile(pathsave2);
}
I can record, stop recording and playback both media recorders separately. When I try to record both at the same time, the app crashes. Any help would be greatly appreciated.
I have something i am really stuck at. The thing is that i am trying to catch the completion when some one is done with sharing something on facebook
i am using the following function to execute and catch the completion. But i am not getting the result.
public void FaceBookSharing() {
Log.d(TAG, "Running facebook share");
Log.d(TAG, "Share on facebook 1: "+sport);
Log.d(TAG, "Share on facebook 2: "+speed);
Log.d(TAG, "Share on facebook 3: "+distance);
Log.d(TAG, "Share on facebook 4: "+date);
Log.d(TAG, "Shared image url: "+sharedImage);
callbackManager = CallbackManager.Factory.create();
final ShareDialog shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.d(TAG, "success");
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "error");
}
#Override
public void onCancel() {
Log.d(TAG, "cancel");
}
});
ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
.setContentTitle("Mijn workout "+sport+" | " +speed+" | "+ distance+" | "+ date+" is gesponsord door "+Company)
.setContentDescription(shareDesc)
.setContentUrl(Uri.parse(mUrl))
.setImageUrl(Uri.parse(sharedImage))
.build();
ShareDialog.show(advertise.this,shareLinkContent);
}
I have searched everywhere and i am unable to find a suiting solution to my problem also i am not really good with listeners.
Thank you.
I have solved as i forgot to init the call back in oncreate.
I am getting this error:
IllegalArgumentException
while performing the below method. I have no idea why it is happening.
Any idea whats wrong here ??
public void sendNoteWithoutImage(){
Toast.makeText(getContext(), "Step 1", Toast.LENGTH_LONG).show();
// saving objects
Note notesRealmClass = new Note();
notesRealmClass.setTitle(titleStr);
Toast.makeText(getContext(), "Step 2", Toast.LENGTH_LONG).show();
ChannelIDs = TextUtils.join(" ",selectedItems);
Toast.makeText(getContext(), "Step 3", Toast.LENGTH_LONG).show();
notesRealmClass.setObjId(objId);
Toast.makeText(getContext(), "Step 4", Toast.LENGTH_LONG).show();
// save object asynchronously
Backendless.Persistence.save(notesRealmClass, new AsyncCallback<Note>() {
public void handleResponse(Note note) {
Toast.makeText(getContext(), "Step 5", Toast.LENGTH_LONG).show();
// new Contact instance has been saved
Toast.makeText(getActivity(), "Successfully posted ", Toast.LENGTH_SHORT).show();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getContext(), "Step 6", Toast.LENGTH_LONG).show();
Log.d("ERROR : ", "" + fault.getMessage());
Log.d("ERROR Code: ",""+fault.getCode());
Toast.makeText(getActivity(), "" + fault.getMessage(), Toast.LENGTH_SHORT).show();
// an error has occurred, the error code can be retrieved with fault.getCode()
}
});}
As you can see i put numbered toasts to check which parts of the codes are executing. From step 1 to 4, everything is fine, but not in step 5. I am getting an error directly on step 6 and the error's print is:
02-18 12:54:09.025 25161-25161/pb.package D/ERRORĀ :: rx/Observable
02-18 12:54:09.025 25161-25161/pb.package D/ERRORĀ Code:: IllegalArgumentException
Your issue should be when creating your Toast inside AsyncCallBack. Because you're using it inside an anonymous class, AsyncCallBack, you can't simply call getContext or getActivity because you are no longer in the scope of the Activity. Try this:
Toast.makeText(NameOfYourActivityClass.this, "Successfully posted ", Toast.LENGTH_SHORT).show();
For example, let's say your Acitivity is called "NotesActivity" then you would do:
Toast.makeText(NotesActivity.this, "Successfully posted ", Toast.LENGTH_SHORT).show();
I'm trying to insert a button to rate app in my activity, with a toast for if market isn't found. But I'm getting a "Context cannot be resolved to a variable" on Activity.this:
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(Activity.this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
}
I've also tried:
Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
But then I get Multiple markers at this line
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
I've made a simple button toast the same way (without try/catch) before, and then it worked fine..
What have I done wrong?
If your class is extending with Activity means use like this
Toast.makeText(ClassName.this, "Couldn't launch the market",Toast.LENGTH_LONG).show();
or
Toast.makeText(getApplicationContext(), "Couldn't launch the market",Toast.LENGTH_LONG).show();
If your Class is extending with Fragment means use like this:
Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();
Your answer:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();
Try:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();
Try this...
Uri uri = Uri.parse("market://details?id="
+ getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't launch the market", Toast.LENGTH_LONG)
.show();
}
});
}
Hope this will help you...