What is wrong with my alarm code?

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. A big concern of mine is that when the user clicks the home button and exits the MainActivity, will my alarm/timer stop starting the service based on the interval, or will the alarm/timer continue to periodically start the IntentService?

Answers

  • elliotyoungelliotyoung USMember

    Please delete this post.
    I found that as a beginner, it was difficult to create an alarm that starts a service. The example did not make sense to me.

    Someone helped me with the problem and this code worked for me in Visual Studio with Xamarin:

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

Sign In or Register to comment.