xamarin.androidbackgroundandroid-emulatorbroadcastreceiverforeground

Xamarin.Android Broadcast Receiver is being fired properly after boot completion on Android Emulator but on Android 9 (Techno Spark 4) it doesn't


I am trying to get my xamarin.android foreground service to run after boot completion on my Android 9 (Techno Spark 4 Air) Mobile Phone. It is working very well on the android emulator.

This is what I have done.

My BroadcastReceiver:

[BroadcastReceiver(Name = "com.companyname.IMEI247Tracker.MyBroadcastReceiver", Enabled = true, Exported = true)]

public class MyBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        //System.String intent_value = intent.GetStringExtra("key");

        Toast.MakeText(context, "Received intent in MyBroadCastReceiver!", ToastLength.Short).Show();

            var intent2 = new Intent(Android.App.Application.Context, typeof(StartServiceAndroid));

            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                Android.App.Application.Context.StartForegroundService(intent2);
            }
            else
            {
                Android.App.Application.Context.StartService(intent2);
            }

    }
}

My Receiver defined inside the Application Tag in Android Manifest XML file:

    <receiver android:name="com.companyname.IMEI247Tracker.MyBroadcastReceiver" android:enabled="true" android:exported="true" android:directBootAware="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="MY_SPECIFIC_ACTION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

My OnResume in MainActivity (this enables MyBroadcastReceiver to kick-start StartAndroidService when my app is running but when the app is killed, the service stops):

       protected override void OnResume()
    {
        base.OnResume();

        if (HelloApplication.GlobalKounter != 1)
        {               
            if (!IsMyServiceRunning(typeof(StartServiceAndroid)))
            {
                RegisterReceiver(receiver, new IntentFilter("MY_SPECIFIC_ACTION"));

                Intent message = new Intent("MY_SPECIFIC_ACTION");

                SendBroadcast(message);
            }
        }

    }

Now, the challenge is this: Whenever I re-start the Techno Spark 9 Mobile Phone, MyBroadcastReceiver is not being triggered to start my foreground service. But, on the Android emulator, everything is working smoothly when I am running the app and when I re-boot the emulator.

I am targeting Android 13 and running Visual Studio 2022.

So, what is really going on and how do I get around this?


Solution

  • I think I have now figured out what is happening with my Techno Spark 4 Android 9 Phone.

    For more than 9hrs and still counting, I have used AlarmManager to ensure my Service is running as it should on the Android Emulator and it has fired the appropriate location emails every 1hr as I scheduled with WorkManager.

    This evening, I setup the app on a Techno Android 8 phone and for more than 1hr and still counting the service is still running and has already fired one location email.

    I have also previously tried the app/service on a Samsung Android 10 and also a Vivo Android 12 phone and it ran on both phones as expected.

    So, what is going on with my Android 9 phone?

    I have turned on OEM unlocking, set the app to auto launch and removed it from battery optimization. Yet, it kept getting killed by Android after about 15 minutes.

    Then, I had a thought about the PhoneMaster Optimizing app (bloatware) on the phone and I went to check its settings page. Among the Settings, is what they termed "Protected Apps." Inside, I saw a list of Apps that the PhoneMaster does not kill their processes anytime it "sweeps" through the system. I then added my app/service to the Protected Apps list.

    I then restarted my app (and after it loaded fully with the service now running, I swipped it off) and as I am writing this Answer, my Service has been running for over 12hours and it doesn't look like it would be getting killed anytime soon.

    I have battled with this issue for over a month and I am just thankful that at least I now understand what was causing this issue all along and have now been able to provide a solution to sort it out.