Android BroadcastReceiver无法接收意图,但静态意图过滤器可以

时间:2019-08-05 20:45:51

标签: android xamarin android-intent broadcastreceiver

我正在编写适应Auth0's library的身份验证模块。在他们的示例中,他们使用静态意图过滤器来获取Web浏览器的登录结果。这行得通,代码看起来像这样:

[IntentFilter( // Adds an <intent-filter> in manifest.xml
    new[] { Intent.ActionView },
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },

    // these are replaced by the correct app info
    DataScheme = "YOUR_ANDROID_PACKAGE_NAME",
    DataHost = "YOUR_DOMAIN",
    DataPathPrefix = "/android/YOUR_ANDROID_PACKAGE_NAME/callback")]
public class MainActivity : Activity { /* intent is received in onNewIntent() */}

现在,我想动态配置身份验证模块,这意味着我不能使用静态意图过滤器,因此我正在尝试使用广播接收器。代码如下:

public class LoginResultReceiver : BroadcastReceiver
    {

        // this is called in MainActivity.onCreate()
        public static void BindToActivity(Activity mainActivity, String dataSheme, String dataHost, String dataPathPrefix)
        {
            // Tried with new IntentFilter(), new IntentFilter(Intent.ActionView) and the following:
            var intentFilter = new IntentFilter(Intent.ActionView);
            intentFilter.AddDataScheme(dataSheme);
            intentFilter.AddDataPath(dataPathPrefix,Pattern.Prefix);
            intentFilter.AddCategory(Intent.CategoryDefault);
            intentFilter.AddCategory(Intent.CategoryBrowsable);

            mainActivity.RegisterReceiver(new LoginResultReceiver(), intentFilter);
        }

        // NEVER CALLED
        public override void OnReceive(Context context, Intent intent)
        {

            Auth0.OidcClient.ActivityMediator.Instance.Send(intent.DataString);

        }
    }

但是,无论我如何初始化IntentFilter,都永远不会调用接收者的OnReceive()方法。有想法吗?

0 个答案:

没有答案