Xamarin Android 应用程序上下文为空

Xamarin Android application context is null

正如我在标题中所说,这是我的问题。为什么我无法访问 class 中的上下文?有什么办法可以在 class?

中获取上下文的实例
[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
class MyGcmListenerService : GcmListenerService
{
    public override void OnMessageReceived(string from, Bundle data)
    {
        string msg = data.GetString("message");

        // the app breaks here
        ShowPopup(msg); 

        Log.Info("GcmLstnrService", "From: " + from);
        Log.Info("GcmLstnrService", "Msg: " + msg);
    }

    private void ShowPopup(string message)
    {
        // the app breaks here
        AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context);

        AlertDialog dialog = builder.Create();
        dialog.SetTitle("Push message");
        dialog.SetMessage(message);
        dialog.SetButton("OK", (sender, e) => { });
        dialog.Show();
    }
}

使用 Forms.Context 代替 Application.Context

您没有可用的 Activity 上下文的原因是这是在服务中并且没有与之关联的 UI。

如果您真的想要显示一个对话框,您有两个选择:

使用基于系统的警报:

var dialog = new AlertDialog.Builder(this).Create();
dialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
dialog.SetTitle("Push message");
dialog.SetMessage(message);
dialog.SetButton("OK", (sender, e) => { });
dialog.Show();

注意:此类警报显示在所有内容之上,因此需要您添加清单权限。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

另一种方法是创建一个新的 Activity 子类,并在其 OnCreate 中创建您的 AlertDialog 并使用 this 作为您的上下文,因为它将是一个 Activity。然后,当您希望 GcmListenerService 显示消息时,创建此 Activity.

的实例

我用 Xamarin.Forms 和 MessagingCenter 解决了这个问题。

这是我的服务:

[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
class MyGcmListenerService : GcmListenerService
{
    public override void OnMessageReceived(string from, Bundle data)
    {
        string msg = data.GetString("message");

        // send a string via Xamarin MessagingCenter
        MessagingCenter.Send<object, string>(this, "ShowAlert", msg);
    }
}

这是我的 PCL App class 构造函数的一部分:

// subscribe to the messages
MessagingCenter.Subscribe<object, string>(this, "ShowAlert", (s, msg) =>
{
    // run on UI thread
    Device.BeginInvokeOnMainThread(() =>
    {
        MainPage.DisplayAlert("Push message", msg, "OK");
    });
});