How to start Xamarin app on android platform at boot time

DUCOS Eric (DEXIP) 116 Reputation points
2021-03-16T17:46:11.42+00:00

Hi, My Xamarin app has a global backgroud thread that needs to be started when the app starts. This is working well on windows platform invoking the background thread on Activated event or Launched event according the way the app is started (I configured the windows app to start automatically on windows login). Now, I want to configure the android app to do the same thing, but as I am not an Android developer, I have difficulties to understand how it could work. Important notice: my target platform is Android 10. After searching on the net, I found this part of code that could work, but it seems that it does not work anymore on recent Android platform (after 8.0); [BroadcastReceiver(Enabled = true, Exported = true)] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class ServiceStarter : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { var mainActivity = new Intent(context, typeof(MainActivity)); mainActivity.AddFlags(ActivityFlags.NewTask); context.StartActivity(mainActivity); } } ..the MainActivity being the standard Xamarin Forms Main Activity that Loads the Xamarin App So please, can someone tell me how I can do the same thing on new android platforms ? I heard about JobIntentService....is that the solution ? How to use it ? Regards. Eric.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-17T07:52:31.707+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Have you added the following permission in file AndroidManifest.xml ?

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  
    

    I tried to create a new demo to achieve this function , and it works properly in my device (Google Pixel 8.1).
    The main code is:

    1.create a BootReceiver

    [BroadcastReceiver(Enabled = true, DirectBootAware = true, Exported = true)]  
    [IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]  
    public class BootReceiver : BroadcastReceiver  
    {  
        public override void OnReceive(Context context, Intent intent)  
        {  
            Intent i = new Intent(context, typeof(MainActivity));  
            i.AddFlags(ActivityFlags.NewTask);  
            context.StartActivity(i);  
        }  
    }  
    

    2.added the following permission in file AndroidManifest.xml

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  
    

    Note: we should change receiver android:name= into above receiver we created (BootReceiver)

    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.


  2. DUCOS Eric (DEXIP) 116 Reputation points
    2021-03-17T09:28:10.057+00:00

    Hi Jessie,

    Yes, I forgot to say that I added also this permission in my manifest as suggested by my others readings...

    I just ask a simple question: how can you add <receiver> tag in you manifest in you Xamarin Forms app ? If I do so (that was I made the first time), I got an error during compilation (or deployment, I do not remember exactly).

    Documentation says that the manifest is automatically updated according to attributes set on the function...and when I go to the "obj" subfolder of my android project, I can find the modified manifest that contains thes lines :

    <application android:largeHeap="true" android:label="ElhomMobilityStorengy" 
                           android:name="crc64d3e6e025a3206700.MainApplication" android:allowBackup="true" android:icon="@drawable/icon" 
                           android:debuggable="true" android:extractNativeLibs="true">
         ....
         <receiver android:enabled="true" android:exported="true" android:name="crc64d3e6e025a3206700.ServiceStarter">
          <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
          </intent-filter>
        </receiver>
        ....
      </application>
    

    I see some difference with your code, for exemple the presence of permission attribute on the rceiver tag...could it be the problem in my case ? I see that I can put a Permission on the BroadCastReciver attribute...

    Regards.