Android AlarmManager Alarm to start Service Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android AlarmManager Alarm to start Service without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Working in VisualStudio with Xamarin and only have < 2 weeks experience with Android.

I have an IntentService class that I want to use an alarm to start. This beauty of IntentService is threading and it will stop when it is finished working. I do not understand how to implement an alarm using AlarmManager. I am following This Tutorial and using the ELAPSED_REALTIME_WAKEUP example.

This code is in the MainActivity under OnCreate()

     AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent intent = new Intent(this, startTrackingService());


     alarmMgr.SetInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,             
     trackingSetting,
     trackingSetting, intent);


     public void startTrackingService() {
     Intent i = new Intent(this, StoreLocationService.class ); //
     StartService(i);
        }

Right now I am getting the following errors:

  • Must have a return type

    StartService(i); 
    
  • Identifier expected; class is a keyword

    Intent i = new Intent(this.StoreLocationService.class );
    

I hope someone can see the glaring errors. I hope that I have the right idea with the Alarm in the MainActivity that uses StartService(Intent Service) to start the service.


Additional question: The service I want to start does a one time Location tracking event. While I want the timer/alarm to work while the screen is off for this tracking, I think that I can therefore not rely on the main activity. Wrong or right?

I have a second service which I will make later. It syncs this data to the local database

Answer

Edit: Ok, after reading up on Xamarin, it looks like this might help for the Identifier expected; class is a keyword error:

StartService(new Intent(this, typeof(StoreLocationService)));

References:

Xamarin android SendBroadcast from IntentService not received by Activity

http://developer.xamarin.com/guides/android/application_fundamentals/backgrounding/part_2_android_services/

Edit 2: I also just noticed that there are some differences with Xamarin with AlarmManagers getSystemService should be GetSystemService, and also it looks like you need a PendingIntent (this is needed for AlarmManager in Java as well).

General example:

Intent i = new Intent(this, typeof(StoreLocationService));
PendingIntent pintent = PendingIntent.GetService(this, 0, i, 0);
AlarmManager alarmMgr = (AlarmManager) this.GetSystemService(Context.AlarmService);
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, trackingSetting, trackingSetting, pintent);

See this post: https://forums.xamarin.com/discussion/16111/setting-alarmmanager-to-repeat-given-a-specific-time

And this post: http://forums.xamarin.com/discussion/7490/alarm-manager

We are here to answer your question about Android AlarmManager Alarm to start Service - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji