Forum Xamarin Xamarin.Android

AlarmManager crashes app when attempting to wake up

arturmullerarturmuller Member ✭✭✭

I implemented a service in Android similar to the solution of my question here: https://stackoverflow.com/questions/61353360/xamarin-service-run-according-to-a-time-schedule/61356731

Initially it works great and I have set that the intent should run every 30min and check if current time is inside user schedule. However, if I don't use the app (minimize) it for a few hours or so it keeps crashing every 30min. I simply get a notification from Android saying " has stopped." and thats it. I have tried enable App Center diagnostics to narrow down what causes the crash. But App Center doesn't recognize the crash. It seems like something is crashing when the AlarmManager is trying to wake up my app after the app has been inactive for a while.

Any ideas what this can be and/or what I can test to try narrow down the problem? I have encapsulated all my code in OnReceive in try-catch statements so it should not be something that is crashing there.

This is how I initialize the AlarmManager

public static void StartScheduleChecker()
        {
            try
            {
                Context context = Application.Context;
                Intent _intent = new Intent(context, typeof(AlarmBroadcastReceiver));
                PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, _intent, 0);
                AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
                alarmManager.Cancel(pendingIntent);

                alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, 0, 1000 * 60 * 30, pendingIntent);

            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }

        }

Really appreciate some guidance! Best regards

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Any ideas what this can be and/or what I can test to try narrow down the problem?

    You could use the adb logcat command or open the Device Log tool to view the debug log for the details about the crash.

    Check the tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/deploy-test/debugging/android-debug-log?tabs=windows

  • arturmullerarturmuller Member ✭✭✭

    Thanks for the reply @YelinZh

    However, for adb logcat or Device Log to work, I guess I need to have it plugged in to my PC at all time? My app is using a background location service so I am testing it while moving so I need to get that log somehow when its not plugged in. Is that possible?

  • JarvanJarvan Member, Xamarin Team Xamurai

    I am testing it while moving so I need to get that log somehow when its not plugged in. Is that possible?

    Yes, the Device Log tool automatically adds log entries from a running app when the device is selected.

Sign In or Register to comment.