Forum Xamarin.Android

ActionBootCompleted BroadcastReceiver fails to call logging class in other .Net project

JVLJVL BEMember ✭✭
edited September 2015 in Xamarin.Android

I recently had a problem with my broadcast receiver who starts a IntentService.
In that intent service i write some information for logging.
When the app is running there is no problem and the logging works fine.
But when i close the app and restart the device, the logging isn't working anymore.
The broadcast receiver works and the intentservice is called but the logging method isn't.
I tried a lot of things but the only thing that solved the problem was moving my static logging class from the .Net project to the Xamarin.Android project.

I don't understand why i can't call a static class from another project within my IntentService. It seems that because the app itself isn't running he can't call the method. Is this normal behaviour?

Receiver

[BroadcastReceiver]
[Android.App.IntentFilter(new[] { Intent.ActionBootCompleted }, Categories = new[] { Intent.CategoryHome })]
[Android.App.IntentFilter(new[] { Intent.ActionUserPresent }, Categories = new[] { Intent.CategoryDefault })]
public class StartupReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (Intent.ActionBootCompleted.Equals(intent.Action) || Intent.ActionUserPresent.Equals(intent.Action)) 
        {
            Intent serviceIntent = new Intent(Application.Context, typeof(ScheduleService));
            context.StartService(serviceIntent);
        }
    }
}

IntentService

[Service]
[IntentFilter(new string[] { AppAgentConstants.ScheduleServiceIntentFilter })]
public class ScheduleService : IntentService
{
    private static readonly string ClassName = string.Format("{0}.{1}", typeof(SoftwareUpdateService).Namespace, typeof(SoftwareUpdateService).Name);

    protected override void OnHandleIntent(Intent intent)
    {
        try
        {
            Logger.WriteToLog("This should be logged"); //--> only works when the app is running (Logger in .Net project and is referenced)
            ServiceManager.Instance.CheckAndSheduleService(this); // --> always works (Is in the Xamarin.Android project)
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(string.Format("ERROR {0}.OnHandleIntent() {1}", ClassName, ex.Message));
        }
    }
}
Sign In or Register to comment.