0

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?

8
  • I don't have a Techno Spark 4 Air device. You can refer to this BroadcastReceiver does not work on Android on how to use BroadcastReceiver. Commented Jan 20, 2023 at 8:46
  • Ok. I have just checked and it is what I have already done that is being suggested. Is there a Tecno firmware setting that I need to turn on or off? Commented Jan 20, 2023 at 20:58
  • I don't think there's a Tecno firmware setting. Moreover, you can go through Broadcast Receivers in Xamarin.Android for more details. Commented Jan 30, 2023 at 8:51
  • My BroadcastReceiver is working properly. I still think it has to do with the Techno settings. In the Developer options on the phone, there is an option OEM unlocking (Allow the bootloader to be unlocked). It is initially disabled. When I enabled it and restarted my phone, my BroadcastReceiver was able to receive the intent "ActionBootComplete" but since then, rebooting the phone is no longer triggering my BroadcastReceiver. Meanwhile, OEM unlocking is not in the Emuator Developer Options. Commented Jan 30, 2023 at 19:18
  • On the Phone, I have also removed the battery optimization attached to my app. Now it is don't optimize, yet it keeps killing it on the phone after a while. Once the app goes down, my foreground service follows suite almost immediately. Commented Jan 30, 2023 at 19:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.