如果将此代码添加到捕获 Android 中 Xamarin.Auth 示例中的重定向 (CustomUrlSchemeInterceptorActivity) 的类中的 OnCreate 方法的末尾,则可以返回到您的应用程序
new Task(() =>{
StartActivity(new Intent(Application.Context,typeof(MainActivity)));
}).Start();
其中 MainActivity 是您在 Android 中的主要 Activity 类的名称。更准确地说,这里是一个完整的类,您可以为拦截的每个重定向继承它
public class UrlSchemeInterceptor : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
base.OnCreate(savedInstanceState);
// Convert Android.Net.Url to Uri
var uri = new Uri(Intent.Data.ToString());
new Task(() =>
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
intent.AddFlags(ActivityFlags.ReorderToFront);
StartActivity(intent);
}).Start();
// Load redirectUrl page
AuthenticationState.Authenticator.OnPageLoading(uri);
Finish();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
public class AuthenticationState
{
public static WebAuthenticator Authenticator;
/*This static field is used to store the object
from OAuth1Authenticator or OAuth2Authenticator
upon initialization in the UI (Xamarin forms or Android or iOS).
For example:
var authenticatorObject = new OAuth2Authenticator (YOUR PARAMETERS);
AuthenticationState.Authenticator = (WebAuthenticator)authenticatorObject;
var presenter = new OAuthLoginPresenter();
presenter.Login(authenticatorObject);
*/
}
例如在谷歌案例中
[Activity(Label = "YOURLABEL", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter( new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[]
{
"com.googleusercontent.apps.",// YOUR GOOGLE ID INVERTED
},
DataPaths = new[]
{
"/oauth2redirect",
})]
public class GoogleUrlSchemeInterceptorActivity : UrlSchemeInterceptor { }