Android homescreeen widget not responding to custom events

epolonskiyepolonskiy RUMember
edited August 2015 in Xamarin.Android

So I created a widget with 1 text and 3 buttons. I want to catch events of the user pressing button. So I did this:

[BroadcastReceiver(Label = "@string/widget_name", Enabled = true, Name = "ru.etreta.shamspay.WordWidget")]
[IntentFilter(new string[] { Helper.ActionClear, Helper.ActionPlus, Helper.ActionMute, Android.Appwidget.AppWidgetManager.ActionAppwidgetUpdate })]
[MetaData("android.appwidget.provider", Resource = "@xml/widget_word")]
public class WordWidget : AppWidgetProvider
{
static int counter = 0;
static RemoteViews updatedViews;
static AppWidgetManager managerWidget;
static int[] appWidgetIds;

    static Intent intent = null;

    #region OnReceive
    string GetCounter()
    {
        string res = WordWidget.counter < 10 ? "00" + WordWidget.counter.ToString() :
            WordWidget.counter < 100 ? "0" + WordWidget.counter.ToString() :
            WordWidget.counter.ToString();

        return res;
    }
    public override void OnReceive(Context context, Intent intent)
    {
        try
        {
            WordWidget.managerWidget = AppWidgetManager.GetInstance(context);
            ComponentName thisWidget = new ComponentName(context, "ru.etreta.shams.WordWidget");
            WordWidget.appWidgetIds = WordWidget.managerWidget.GetAppWidgetIds(thisWidget);

            CreateViews(context);

            switch (intent.Action)
            {
                case Helper.ActionClear:
                    {
                        WordWidget.counter = 0;

                        WordWidget.updatedViews.SetTextViewText(Resource.Id.wText, GetCounter());

                        foreach (int i in WordWidget.appWidgetIds)
                            WordWidget.managerWidget.UpdateAppWidget(i, WordWidget.updatedViews);
                        break;
                    }
                case Helper.ActionPlus:
                    {
                        WordWidget.counter++;
                        WordWidget.updatedViews.SetTextViewText(Resource.Id.wText, GetCounter());

                        foreach (int i in WordWidget.appWidgetIds)
                            WordWidget.managerWidget.UpdateAppWidget(i, WordWidget.updatedViews);
                        break;
                    }
                case Helper.ActionMute:
                    {
                        var am = (AudioManager)context.GetSystemService(Context.AudioService);

                        if (am.RingerMode == RingerMode.Silent || am.RingerMode == RingerMode.Vibrate)
                        {
                            Helper.GotoBack(context);

                            WordWidget.updatedViews.SetInt(Resource.Id.wbutton3, "setBackgroundResource", Resource.Drawable.sound11);
                            foreach (int i in WordWidget.appWidgetIds)
                                WordWidget.managerWidget.UpdateAppWidget(i, WordWidget.updatedViews);
                        }
                        else
                        {
                            Helper.SavePrev(context);
                            Helper.GotoSilence(context);

                            WordWidget.updatedViews.SetInt(Resource.Id.wbutton3, "setBackgroundResource", Resource.Drawable.sound12);
                            foreach (int i in WordWidget.appWidgetIds)
                                WordWidget.managerWidget.UpdateAppWidget(i, WordWidget.updatedViews);
                        }
                        break;
                    }
            }
        }
        catch (Exception ex) { Log.Error("WordWidget.OnReceive", ex.Message); }

        base.OnReceive(context, intent);
    }

    void CreateViews(Context context)
    {
        WordWidget.updatedViews = new RemoteViews("ru.etreta.shams", Resource.Layout.widget);
        WordWidget.updatedViews.SetTextViewText(Resource.Id.wText, GetCounter());

        var am = (AudioManager)context.GetSystemService(Context.AudioService);
        if (am.RingerMode == RingerMode.Silent || am.RingerMode == RingerMode.Vibrate)
        {
            WordWidget.updatedViews.SetInt(Resource.Id.wbutton3, "setBackgroundResource", Resource.Drawable.sound12);
        }
        else
        {
            WordWidget.updatedViews.SetInt(Resource.Id.wbutton3, "setBackgroundResource", Resource.Drawable.sound11);
        }

        if (WordWidget.intent == null)
        {
            WordWidget.intent = new Intent(context, typeof(WordWidget));
        }

        // Clear button
        WordWidget.intent.SetAction(Helper.ActionClear);
        PendingIntent actionPendingIntent = PendingIntent.GetBroadcast(context, 0, WordWidget.intent, PendingIntentFlags.UpdateCurrent);
        WordWidget.updatedViews.SetOnClickPendingIntent(Resource.Id.wbutton2, actionPendingIntent);

        // +1 button
        WordWidget.intent.SetAction(Helper.ActionPlus);
        PendingIntent actionPendingIntent2 = PendingIntent.GetBroadcast(context, 0, WordWidget.intent, PendingIntentFlags.UpdateCurrent);
        WordWidget.updatedViews.SetOnClickPendingIntent(Resource.Id.wbutton1, actionPendingIntent2);

        // mute button
        WordWidget.intent.SetAction(Helper.ActionMute);
        PendingIntent actionPendingIntent3 = PendingIntent.GetBroadcast(context, 0, WordWidget.intent, PendingIntentFlags.UpdateCurrent);
        WordWidget.updatedViews.SetOnClickPendingIntent(Resource.Id.wbutton3, actionPendingIntent3);


        WordWidget.managerWidget.UpdateAppWidget(WordWidget.appWidgetIds, WordWidget.updatedViews);
    }

    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {

        WordWidget.managerWidget = AppWidgetManager.GetInstance(context);
        ComponentName thisWidget = new ComponentName(context, "ru.etreta.shams.WordWidget");
        WordWidget.appWidgetIds = WordWidget.managerWidget.GetAppWidgetIds(thisWidget);

        CreateViews(context);
    }

The problem is that OnReceive() method is called only when theese Intent actions occur: Enabled, Update, Deleted, Disabled. But I need such intents as:
Helper.ActionClear, Helper.ActionPlus, Helper.ActionMute to be happened too. How can I achieve this?

Answers

  • epolonskiyepolonskiy RUMember

    I managed to do this. So the problem was that I was creating widget intents in the namespace different from one I was listening to in BroadcastReceiver

Sign In or Register to comment.