Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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. Then when you want your GcmListenerService to display a message create an instance of this Activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...