Widget not working without launched app

Hi! I'm new here, and I 'm programming in xamarin since 3 weeks. And now I have first bigger problem - my widget don't want working without lauched app. So I'm installing my app by visual, app is launching, I minimalize app to background and adding the widget. As long as app is running in memory widget is working, but if I clean cache widget is stopping. And if i add widget without started application in background, his layout remain such as initial layout. Please help!
This is my code.

Widget.cs:
[BroadcastReceiver (Label = "IleDoPrzerwy")] [IntentFilter (new string [] { "android.appwidget.action.APPWIDGET_UPDATE" })] [MetaData ("android.appwidget.provider", Resource = "@layout(slash)widget_conf")]
`public class Widget : AppWidgetProvider
{

        public override void OnEnabled(Context context)
        {
            Timetable.LoadStatic();

            AlarmManager alarm = (AlarmManager)context.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(context, typeof(UpdateBroadcast));
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, 0);
            alarm.SetRepeating(AlarmType.Rtc, /*SystemClock.CurrentThreadTimeMillis()*/ 100, 60000, pendingIntent);
            context.SendBroadcast(intent);
        }

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

        public override void OnDisabled(Context context)
        {
            AlarmManager alarm = (AlarmManager)context.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(context, typeof(UpdateBroadcast));
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, 0);
            alarm.Cancel(pendingIntent);
        }  
    }`

UpdateBroadcast.cs:
`
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "com.ChosenButter.IleDoPrzerwyWidget.UPDATE" })]
class UpdateBroadcast : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Update(context);
}

        void Update(Context context)
        {
            bool breakTime, afterLessons;
            TimeSpan counter = TimeCounter.Widget(out breakTime, out afterLessons);
            AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
            ComponentName component = new ComponentName(context, Java.Lang.Class.FromType(typeof(Widget)).Name);

            // Build an update that holds the updated widget contents
            var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
            updateViews.SetTextViewText(Resource.Id.WCounter, counter.ToString(@"hh\:mm"));
            updateViews.SetTextViewText(Resource.Id.WToEnd, (breakTime || afterLessons) ? "Do lekcji" : "Do przerwy");
            appWidgetManager.UpdateAppWidget(component, updateViews);
        }
    }

`

widget_conf.xml in Resources(slash)layout:
`
<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http:(slash)(slash)schemas.android.com(slash)apk(slash)res(slash)android"
    android:minWidth="294dip"
    android:minHeight="72dip"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget" />

`

And I was found one bug: if I placed widget_conf.xml in Resource(slash)xml app was crashing on start with NullReferenceException in MainActivity.cs file in line
LoadApplication(new App());. Please fix it!

PS. I don't know how to place here code correctly so it can be ugly.

Tagged:

Answers

Sign In or Register to comment.