Xamarin.Forms 替代 StartActivityForResult
Xamarin.Forms alternative to StartActivityForResult
我目前正在尝试从 RingtoneManager.ActionRingtonePicker
中选择 Uri。我输入了所有额外设置并设置了适当的标志。
但是我只能调用 StartActivity(intent)
。无论如何,我是否可以从铃声选择器中获取选择并使用 intent.getParceableExtra(RingtoneManager.ExtraRingtonePickedUri);
。我的代码在下面,如果我可以用变通方法替换 context.StartActivity(intent)
那就太好了。
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;
}
实际上,Xamarin.Forms存在于Activity中,Activity中的OnActivityResult方法仍然可以调用。
在您的 DependencyService
class 中,使用 StartActivityForResult()
方法,如下所示:
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);
}
然后,您可以像这样在 OnActivityResult
方法中接收结果:
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;
}
}
}
}
我目前正在尝试从 RingtoneManager.ActionRingtonePicker
中选择 Uri。我输入了所有额外设置并设置了适当的标志。
但是我只能调用 StartActivity(intent)
。无论如何,我是否可以从铃声选择器中获取选择并使用 intent.getParceableExtra(RingtoneManager.ExtraRingtonePickedUri);
。我的代码在下面,如果我可以用变通方法替换 context.StartActivity(intent)
那就太好了。
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;
}
实际上,Xamarin.Forms存在于Activity中,Activity中的OnActivityResult方法仍然可以调用。
在您的 DependencyService
class 中,使用 StartActivityForResult()
方法,如下所示:
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);
}
然后,您可以像这样在 OnActivityResult
方法中接收结果:
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;
}
}
}
}