I am trying to ask users to select their google account here. After I use the "setSelectedAccoutName" method, i try to print out the selected account name which returns null. I am 100% sure that accountName is not null. So does anyone know why mCredential.getSelectedAccountName is null?
private void pickContact() {
AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
Account[] accounts = accountManager.getAccountsByType(null);
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, 23);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("hi");
if (requestCode == 23) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
String accountName = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
System.out.println(accountName);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
System.out.println(mCredential.getSelectedAccountName());
getResultsFromApi();
} else {
pickContact();
}
}
}
}
Related
I was trying to select an image from a device folder but it an error in the logcat like this:
(https://i.stack.imgur.com/0yFm8.jpg)
(https://i.stack.imgur.com/owWL4.jpg)
This error doesn't always come out and sometimes it changes, but it's always about running out of memory
This is the code with which I choose an image in the gallery:
public void inserimentoImmagine() {
addImage = findViewById(R.id.aggiuntaFoto);
addImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (ActivityCompat.checkSelfPermission(AggiuntaAnimaleManuale.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AggiuntaAnimaleManuale.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
} else {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PICK_FROM_GALLERY:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
} else {
//do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
}
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
if (data == null) {
//Display an error
return;
}
Uri imgUri= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor=getContentResolver().query(
imgUri, filePathColumn, null, null, null
);
String picturePath;
Bitmap bitmap;
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
}
}
}
}
the problem when I click on Button choose a contact and choose a contact not call the number, I want when choosing a Number phone call Directly
buttoncontact = findViewById(R.id.choosecontact);
public void choosecontact1 (View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT)
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String call = buttoncontact.getText().toString();
if ((requestCode == 1) && (resultCode == RESULT_OK)) {
Cursor cursor = null;
try {
Uri uri = data.getData();
cursor = getContentResolver().query(uri, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
String phone = cursor.getString(0);
// the Problem on that code
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(String.valueOf("tel:" + CONTENT_TYPE)));
// i want open phone contact and call directly
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
What is "CONTENT_TYPE"? Shouldn't it be the phone string you just get?
intent.setData(Uri.parse("tel:" + phone));
Add permission in manifest file :
Make function of calling phone number
private static final int CALL_REQUEST = 100;
private void callPhoneNumber(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ProfileActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_REQUEST);
return;
}
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9879879879" ));
startActivity(callIntent);
}
Above api level 23, need to ask runtime permission of call and if user allow it, it will go in below method :
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults)
{
if(requestCode == CALL_REQUEST)
{
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callPhoneNumber();
}
else
{
Toast.makeText(ProfileActivity.this, "Permission Denied", 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'm trying to get a contacts name from my contacts by using an Intent, it seems the method I found earlier is deprecated and when I try to use it my app force quits. I'm new to Android development and any help would be appreciated.
public void openContacts(View view) {
Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactsIntent, 1);
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if(c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(Contacts.People.NAME));
TextView contactName = (TextView)findViewById(R.id.contact_name_textview);
contactName.setText(name);
}
}
}
}
I'm calling the openContacts function as an onClick event for a button. Once I've picked my contact I would like to set the name to a TextView that I have already made.
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();
}
}