Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

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

$
0
0

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
}

}
}`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>