question

DUCOSEricDEXIP-6148 avatar image
0 Votes"
DUCOSEricDEXIP-6148 asked RobCaplan edited

How to start Xamarin app on android platform at boot time

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.

dotnet-xamarin
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.

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

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.







· 4
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.

Hi @DUCOSEricDEXIP-6148 , for xamarin forms app, it is just the same as we do in xamarin android. We just need to add BootReceiver in android platform and add <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> in file AndroidManifest.xml . I did a test, it works properly. You can try it.

0 Votes 0 ·

Hi Jessie,

Well, tio be sure that my cirrent project does not infer with other things, I just create a very new Xamarin Forms project without changing anything.

I juste added permission to Android project and added a new class with the code you mentioned.

I can says that it works...BUT...only on my Samsung Galaxy S2 with Android 7.

My last target platform is a Samsung Galaxy S5e with Android 10, and on this device, the app does not start when I reboot the device...

It seems that what is mentioned in different places of the documentation is right. Starting Android 8, we cannot do that in this way...and I did not find another way to do it !!!!

Regards

0 Votes 0 ·

I tested on google pixel (android 8.1), it works. From API29 Android restricted to start an activity from the background. You can check Restrictions on starting activities from the background .


0 Votes 0 ·

Since I don't have Samsung Galaxy S5e with Android 10 to test, but you can also try to enable auto start permission in your phone settings. And there is a similar thread about this, you can check it: https://stackoverflow.com/questions/39366231/how-to-check-miui-autostart-permission-programmatically/49167712#49167712

0 Votes 0 ·
DUCOSEricDEXIP-6148 avatar image
0 Votes"
DUCOSEricDEXIP-6148 answered JessieZhang-2116 edited

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.


· 1
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.

Yes, Xamarin.Android will automatically register the service in the manifest at build time with the necessary XML attribute. Sorry, I forgot to remove it.
I have updated my answer. It works properly. And we can also set the Priority to HighPriority. You can check my answer.

0 Votes 0 ·