简体   繁体   English

Xamarin Android 应用程序上下文为空

[英]Xamarin Android application context is null

As I said in the heading, this is my problem.正如我在标题中所说,这是我的问题。 Why can't I access the context in my class?为什么我无法访问班级中的上下文? Is there any way I could get the instance of the context in this 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();
    }
}

The reason that you do not have an Activity context available is this is within a service and there is no UI associated to it.您没有可用的 Activity 上下文的原因是它位于服务中,并且没有与之关联的 UI。

If you really want to show a dialog, you have two options:如果你真的想显示一个对话框,你有两个选择:

Use a System-based alert:使用基于系统的警报:

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();

Note: This type of alert gets displayed on top of EVERYTHING and thus requires that you add manifest permissions.注意:这种类型的警报显示在一切之上,因此需要您添加清单权限。

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

The other way is to create a new Activity subclass and in its OnCreate create your AlertDialog and use this as your context since it will be an Activity.另一种方法是创建一个新的Activity子类,并在其OnCreate创建您的AlertDialog并将this用作您的上下文,因为它将是一个 Activity。 Then when you want your GcmListenerService to display a message create an instance of this Activity .然后,当您希望GcmListenerService显示消息时,请创建此Activity的实例。

使用Forms.Context代替Application.Context

I resolved this issue with Xamarin.Forms and MessagingCenter.我使用 Xamarin.Forms 和 MessagingCenter 解决了这个问题。

Here's my service:这是我的服务:

[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);
    }
}

And here's part of my PCL App class constructor:这是我的 PCL App 类构造函数的一部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM