Background service doesn't work on release mode ???

Hi everyone,
I created an app with periodic background service (with alarm manager), this service launch every x minutes, so until now, everything is fine, I can start or stop my service without any problems.. until I deployed the app with release mode, after that whatever I start the service the app crashes!
any suggestions ? thanks

This is my code..

Start/Stop service from shared project

DependencyService.Get<IAndroidNotifications>().StartNotificationsService();
DependencyService.Get<IAndroidNotifications>().StopNotificationsService();

Notifications class

`public void StartNotificationsService()
{
//Start Notifications Service
Intent myIntent = new Intent(context, typeof(NotificationsService));
context.StartService(myIntent);


    //Show Snackbar message
    Activity activity = CrossCurrentActivity.Current.Activity;
    Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
    Snackbar.Make(activityRootView, "Notifications on", Snackbar.LengthLong).Show();
}

public void StopNotificationsService()
{
    //Stop Notifications Service
    Intent myIntent = new Intent(context, typeof(NotificationsService));
    context.StopService(myIntent);


    //Show Snackbar message
    Activity activity = CrossCurrentActivity.Current.Activity;
    Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
    Snackbar.Make(activityRootView, "Notifications off", Snackbar.LengthLong).Show();
}

public bool CheckNotificationsService()
{
    ActivityManager manager = (ActivityManager)context.GetSystemService(Context.ActivityService);
    foreach (var service in manager.GetRunningServices(int.MaxValue))
    {
        string x = service.Service.ClassName;
        if (service.Service.ClassName == "com.TestApp.NotificationsService") 
        {
            return true;
        }
    }
    return false;
}

private void StartAlarm(bool isRepeating)
{
    AlarmManager manager = (AlarmManager)GetSystemService(Context.AlarmService);
    Intent myIntent;
    PendingIntent pendingIntent;


    myIntent = new Intent(this, typeof(NotificationsAlarm));
    pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);


    if (!isRepeating)
    {
        manager.Set(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 3000, pendingIntent);
    }
    else
    {
        manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 3000, 60 * 1000, pendingIntent);
    }

}
}`

Notification service

`[Service
(Name = "com.TestApp.NotificationsService",
Label = "Service Notifications")]
class NotificationsService : Service
{
AlarmManager manager;
Intent myIntent;
PendingIntent pendingIntent;

public override IBinder OnBind(Intent intent)
{
    return null;
}

//[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
    //return base.OnStartCommand(intent, flags, startId);
    manager = (AlarmManager)GetSystemService(Context.AlarmService);



    myIntent = new Intent(this, typeof(NotificationsAlarm));
    pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);

    manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 1000, 60 * 1000, pendingIntent);


    //Toast.MakeText(this, "Greating from our first service", ToastLength.Long).Show();
    return StartCommandResult.Sticky;
}

public override void OnDestroy()
{
    base.OnDestroy();
    pendingIntent.Cancel();
    manager.Cancel(pendingIntent);
}

}
}`

Broadcast receiver

`namespace TestApp.Droid
{
[BroadcastReceiver(Enabled = true)]
public class NotificationsAlarm : BroadcastReceiver
{
static Random random = new Random();
int randomNumber = random.Next(0, 1000);

InfosAPI _infosAPI;
public override void OnReceive(Context context, Intent intent)
{
    Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();

    //something else here
}

}
}`

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    What is your Linker Options or ProGuard when release your application like this linker?
    https://xamarinhelp.com/xamarin-linker/
    If you set the Linker Options, please select to none, then re-run your project.
    You could see the full stack trace from logcat. If you have exception about error: package android.xxxxx does not exist or miss ....class * extends android.app.xxxx.

  • ArselanArselan Member ✭✭

    @LeonLu hey thanks so much, I just changed the Dex compiler from [dx] to [d8] and the service starts successfully now!
    just one more question.. it seems like I can't stop the service now !! the only way to stop it is to uninstall the app :neutral:
    Is there something wrong in my code ? or it's also related to the linker/shrinker?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Did you stop the service in your code?

    A client (such as an Activity) can request a service stop by calling the StopService or A service may shut itself down by invoking the StopSelf

  • MBatMBat Member ✭✭

    @LeonLu if i want to write Xamarin(android) App independent of any application that run in the background periodically updating some data on the server would i use Service ,alarm manager or something else ?

Sign In or Register to comment.