I am currently trying to get the Uri selected from a RingtoneManager.ActionRingtonePicker. I put in all my extra settings and set the appropriate flag.
However I am only able to call StartActivity(intent). Is there anyway for me to get the selection from the Ringtone Picker and use the intent.getParceableExtra(RingtoneManager.ExtraRingtonePickedUri);. My code is below and if I can replace the context.StartActivity(intent) with a work around then that would be great.
public async Task<string> pickAndReceiveRingtone(string currentUri)
{
Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
intent.SetFlags(ActivityFlags.NewTask);
//Replace below
/*await*/ context.StartActivity(intent);
//Grab selected uri here
currentUri = ...
return currentUri;
}
Actually, Xamarin.Forms exists in an Activity, OnActivityResult method in Activity still can be called.
In your DependencyService class, use the StartActivityForResult() method like this :
public void StartActivityInAndroid()
{
Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
//intent.SetFlags(ActivityFlags.NewTask);// remember to delete this
var activity = Forms.Context as Activity;
activity.StartActivityForResult(intent, 0);
}
Then, you can receive the result in OnActivityResult method like this :
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode != Result.Ok)
{
return;
}
else
{
Android.Net.Uri uri = (Android.Net.Uri)data.GetParcelableExtra(RingtoneManager.ExtraRingtonePickedUri);
Log.Debug("onActivityResult====", "" + uri);
Toast.MakeText(this, uri + "", ToastLength.Short).Show();
if (uri != null)
{
switch (requestCode)
{
case 0:
RingtoneManager.SetActualDefaultRingtoneUri(this, RingtoneType.Ringtone, uri);
break;
}
}
}
}
Related
I am trying to launch an activity after a user has selected a photo. I was trying to do this:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
}
}
But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.
I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().
I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().
If you are in Activity then you can just use this as Context
Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);
If you are in a Fragment then you can obtain Context by calling getContext()
Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);
And use that code inside onActivityResult() as you were trying to.
set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:
if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
if (data == null) {
//Display an error
println("error accuered at onActivityResult ")
return
}
Have you tried this simpler one:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
startActivity(goToActivityIntent);
}
}
Just call
startActivity(goToActivityIntent);
to call the activity.
This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.
I am wondering if using a Chooser means you can't send extra custom data to the receiving intent?
I call a file selection intent like this
public static void receiveFiles(final AppCompatActivity activity, int which) {
Intent receiveIntent = new Intent(Intent.ACTION_GET_CONTENT);
receiveIntent.setType("text/comma-separated-values");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
receiveIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
receiveIntent.addCategory(Intent.CATEGORY_OPENABLE);
receiveIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
receiveIntent.putExtra(IMPORT_OPTION_WHICH, which); //this line
activity.startActivityForResult(Intent.createChooser(receiveIntent, "Pick CSV"), REQUEST_IMPORT_CSV);
}
But then in onActivityResult after the user selects a file:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMPORT_CSV:
if (data != null) {
Uri uri = data.getData(); //is correct
int which = data.getIntExtra(IMPORT_OPTION_WHICH, -1);
//but this always -1!
}
break;
}
}
}
How can I get it to correctly extract the value I passed into the intent?
i have button which have attribute android:onClick="atnDuom".
There is that function
public void atnDuom(View view)
{
finish();
}
and there is onActivityResult function in the same activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
DOP = new DatabaseOperations(ctx);
Intent returnIntent = new Intent();
user_name = data.getStringExtra("tarpVard");
user_lastname = data.getStringExtra("tarpPav");
institucijos_pavadinimas = data.getStringExtra("tarpInst");
padalinio_pavadinimas = data.getStringExtra("tarpPad");
pareigos = data.getStringExtra("tarpPar");
mob_tel = data.getStringExtra("tarpMob");
el_pastas = data.getStringExtra("tarpEl");
setResult(RESULT_OK,returnIntent);
DOP = new DatabaseOperations(ctx);
if(newVard.equals("")||newPav.equals("")||newInst.equals("")||newPad.equals("")||newPar.equals("")||newMob.equals("")||newEl.equals(""))
{
Toast.makeText(getBaseContext(), R.string.prashome, Toast.LENGTH_LONG).show();
}
else
{
DOP.updateUserInfo(DOP, user_name, user_lastname, institucijos_pavadinimas, padalinio_pavadinimas, pareigos, mob_tel, el_pastas, newVard, newPav, newInst, newPad, newPar, newMob, newEl);
Toast.makeText(getBaseContext(), "Duomenys atnaujinti", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
It is possible to execute function onActivityResult whithout doing anything in atnDuom function?
Finish() close activity and onActivityResult doesnt work :)
You are using data from the intent, if you want to go to onActivityResult from atnDuom you will need to create a new Intent and push all the data needed
Intent newIntent = new Intent();
newIntent.putExtras(...);
onActivityResult(REQUEST_CODE, RESULT_OK, newIntent);
I'm doing a few dialogs pickers on my app and using a switch-case on my onClickListener but dialogs are calling for a onActivityResult{} but app is crashing if I add 2... Can I put them both in the same onActivityResult{} calling each by results codes? Code below:
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bContacts:
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
break;
case R.id.bRingtone:
String uri = null;
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
startActivityForResult( intent, Set_Ringtone);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
Cursor c = getContact(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
if (c.moveToNext()) {
String name = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
callName.setText(name);
String phoneNumber = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
callNum.setText(phoneNumber);
Log.d("Cont", "name "+name+" no. " + phoneNumber);
}
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
String ringTonePath = uri.toString();
Toast.makeText(this, "GOT IT" + ringTonePath, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "DIDNT", Toast.LENGTH_SHORT).show();
}
}
}
You can handle both intents in the same onActivityResult() method. That's why you send a request code with your intent when you call startActivityForResult(), you get it back when the result comes in to differentiate. Your method could look like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case PICK_CONTACT:
// handle the contact result
break;
case Set_Ringtone:
// handle the ringtone result
break;
}
}
Your app most likely crashes because you try to work with details of the result that are not provided if you select the wrong intent (e.g. you try to read a contact name here, a ringtone result will not work with that).
I have this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
startActivityForResult(intent, PICK_CONTACT);
Then on response:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
try {
Uri contactData = data.getData();
String onlineid = data.getStringExtra(EXTRA_ONLINE_ID);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
super.onActivityResult(reqCode, resultCode, data);
}
the onlineid variable is null. How can I pass a value and then to receive it back?
EDIT
I even tried,
Bundle extras = data.getExtras(); // returns null
This is done by design; system activities will not send back the extras with which they're called, so you have to manage the data elsewhere.
Luckily, the resultCode parameter is fully controlled by yourself, which means that you can use it to index your data.
private final int PICK_CONTACT = 0;
private Bundle[] myDataTransfer = { null };
...
Bundle myData = new Bundle();
myData.putString(EXTRA_ONLINE_ID, (String) v.getTag());
myDataTransfer[PICK_CONTACT] = myData;
// create intent and all
startActivityForResult(intent, PICK_CONTACT);
...
public void onActivityResult(int reqCode, int resultCode, Intent data) {
if (resultCode == PICK_CONTACT) {
Bundle myData = myDataTransfer[resultCode];
String onlineid = myData.getString(EXTRA_ONLINE_ID);
}
}
I'm not a Java programmer, there must be a nicer way to implement a map of Bundles, but this works :)
ok Check if your Activity android:launchMode is configured as SingleTask or SingleInstance! that must be the problem :)
The EXTRA_ONLINE_ID field will have to be set in the activity that you launched using setResult. If it's not setting that value in the returned Intent (which is different from what you sent) then you will get a null value.
I was running into some problems with this as well.
Instead of this line
intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
Try
intent.putExtra(EXTRA_ONLINE_ID, "" + v.getTag());