question

AravapalliManikishore-1338 avatar image
0 Votes"
AravapalliManikishore-1338 asked JessieZhang-2116 answered

Calling Foreground service from Broadcast Receiver when app was closed / killed

Hi Everyone,

I've problem in calling the Foreground Service from the Broadcast Receiver when the app was killed by user. It is working fine when the app is open or recent apps.

Actually, my requirement is to track the location for every X minutes even the app is killed / closed.
So, I created a Broadcast Receiver for Location service. And It is not working in Android 7 when the app got killed.
So, I created a Repeating Alarm and calling foreground service from alarm Receiver. It is working fine when the app is open. But not when app got killed.

I make the Alarm Receiver as

     <receiver android:process=":remote" android:name="ScheduledAlarmHandler"></receiver>

to run process even app got killed

Code to Alarm Receiver

    public async override void OnReceive(Context context, Intent intent)
     { 
         if (Build.VERSION.SdkInt < BuildVersionCodes.O)
         {
             if (ActivityCompat.CheckSelfPermission(context, Android.Manifest.Permission.AccessFineLocation) == Android.Content.PM.Permission.Granted)
             {
                 var msg = new StartServiceMessage();
                 Xamarin.Forms.MessagingCenter.Send<StartServiceMessage>(msg, "ServiceStarted");
             }
         }
  }


Can anyone help me with right approach.

Thanks in Advance.

dotnet-xamarinformsdotnet-androiddotnet-xamarinessentials
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered

Hello,


Welcome to our Microsoft Q&A platform!

You can try to restarted the service once it been finished by a BroadcastReceiver .
The function of BroadcastReceiver is to receive a signal when someone or something kills the service; its role is to restart the service.

For more details, you can check:
https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
Though it's Java code, it's easy to understand and convert.


Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.