Auto-Start Foreground Service in Android Xamarin

Vuyiswa Maseko 351 Reputation points
2021-12-03T21:03:11.647+00:00

Good Day All

I have a Foreground Service that works well . but now When a Phone is restarted i would like to start the service as well

i have created a broadcast receiver as defined below

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    { 
        Intent i = new Intent(context, typeof(startServiceAndroid));
        i.AddFlags(ActivityFlags.NewTask);
        context.StartService(i);
    }
}

and my manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.foregtrova" android:installLocation="auto">
 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
 <application android:label="ForeGTrova.Android" android:theme="@style/MainTheme">
     <service android:name=".startServiceAndroid" />
    <!-- Start the Service if applicable on boot -->
    <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
      <intent-filter>
        <category android:name="android.intent.category.DEFAULT"/>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <!--For HTC devices-->
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
      </intent-filter>
    </receiver>
  </application> 
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 <uses-permission android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

i have installed the app on my phone, Restarted the phone , the Service does not start on boot. i have to open the app and close it for it to keep running.

Thanks

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-12-07T08:29:39.92+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    On Android 8.0 and above, we need to use Context.StartForegroundService to start a foreground service.

       [BroadcastReceiver(Enabled = true)]  
       [IntentFilter(new[] { Intent.ActionBootCompleted })]  
       public class BootReceiver : BroadcastReceiver  
       {  
           public override void OnReceive(Context context, Intent intent)  
           {  
               Intent i = new Intent(context, typeof(BootService));  
               i.AddFlags(ActivityFlags.NewTask);  
               if (Build.VERSION.SdkInt >= BuildVersionCodes.O)  
               {  
                   context.StartForegroundService(i);  
               }  
               else  
               {  
                   context.StartService(i);  
               }  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Vuyiswa Maseko 351 Reputation points
    2021-12-07T10:45:03.717+00:00

    Thank you very much its working now. i restarted the device and the service is running .

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.