Forum Xamarin.Android

Problems with Services after restart the device in Android 9.0

ephramdephramd ESMember ✭✭

Hi,

  • I'm using a Xiaomi MI 8 with Android 9.
  • I have created a SERVICE that communicates with a server and with JS functions launch native actions.
  • For example: It launches a toast and a notification every 5 seconds. (This works while the app is open).
  • I see a new notification and toast every 5 seconds.

Problems:

  • If I close the application the service stops. No more toast & notifications...
  • If I turn the device off and on, the toast and 1 notification appear. (It seems to work but ...)
    But only 1 time! In the server JS the toast and the notification are thrown in a 5 second interval.

And the broadcastreceiver has the following structure:

[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
    [IntentFilter(new[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class BootReceiver : BroadcastReceiver {
        public override void OnReceive(Context context, Intent intent) {
            CustomHelper.StartService(true); // This call a service with: Application.Context.StartForegroundService (new Intent (intent));
        }
    }

And I use Android.Manifest.Permission.ForegroundService.

I have read about the limitations in the documentation but maybe I do not apply it well.

**Can someone show me a complete example code? Thanks!
**
https://developer.android.com/about/versions/oreo/background.html#broadcasts
https://developer.android.com/topic/performance/background-optimization.html

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    If I close the application the service stops. No more toast & notifications... And I use Android.Manifest.Permission.ForegroundService.

    ForegroundService will not work when the app in background.

    If I turn the device off and on, the toast and 1 notification appear. But only 1 time!

    Could you post the related code?

  • AnandmauryaAnandmaurya INMember ✭✭

    @ephramd Use JobIntenService for Android OS Oreo and above to run the application in background and foreground.

    https://forums.xamarin.com/discussion/134197/any-xamarin-android-c-sample-for-jobintentservice

    And also add Broadcast Receiver Intent Filter dynamically.

  • ephramdephramd ESMember ✭✭

    @yelinzh @Anandmaurya
    thanks for answering and orget what I mentioned.

    I just want a BroadcastReceiver that after turning on the device (or removing the power cable) start a service without using a fixed notification (as foreground does) and that works on Android 9.0.

    Can anyone share a simple example code with the structure code (Manifest.xml, Main.cs, Service.cs and BroadcastReceiver.cs)?

    I am reading the documentation but I do not see any full example, I have nothing clear how to make it work in the current version of android.

  • ephramdephramd ESMember ✭✭
    edited June 2019

    @yelinzh @Anandmaurya I tried this:

    • RegistrationIntentService works, but if I close the app it is also finished.
    • CustomBroadcastReceiver does nothing.

    MainActivity.cs:
    protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);

                Console.WriteLine("------------------ OnCreate service started ");
                Log.Info("Fran", "------------------ OnCreate service started");
                Toast.MakeText(Application.Context, "OnCreate service started", ToastLength.Short).Show();
    
                Intent service = new Intent(this, typeof(RegistrationIntentService));
                service.PutExtra("prueba", "fran");
                RegistrationIntentService.EnqueueWork(this, service);
    
            }
    

    AndroidManifest.xml








    RegistrationIntentService:
    [Service(Name = "com.MyPackageName.RegistrationIntentService", Permission = "android.permission.BIND_JOB_SERVICE", Exported = true)]
    public class RegistrationIntentService : Android.Support.V4.App.JobIntentService {

        private static int MY_JOB_ID = 1000;
    
        public static void EnqueueWork(Context context, Intent work) {
    
            Java.Lang.Class cls = Java.Lang.Class.FromType(typeof(RegistrationIntentService));
    
            Console.WriteLine("------------------ EnqueueWork start ");
            Log.Info("TEST", "------------------ EnqueueWork start");
            Toast.MakeText(Application.Context, "EnqueueWork start", ToastLength.Short).Show();
    
            try {
                EnqueueWork(context, cls, MY_JOB_ID, work);
            }
            catch (Exception ex) {
                Console.WriteLine("------------------ EnqueueWork Exception: {0}", ex.Message);
                Log.Info("TEST", "------------------ EnqueueWork Exception: {0}");
                Toast.MakeText(Application.Context, "EnqueueWork Exception: {0}", ToastLength.Short).Show();
            }
    
        }
    
        protected override void OnHandleWork(Intent p0) {
            Console.WriteLine("------------------ OnHandleWork service started ");
            Log.Info("TEST", "------------------ OnHandleWork service started");
    
            Task.Run(async () => {
    
    
                int i = 1;
                while(i > 0)  {
                    Console.WriteLine("------------------ OnHandleWork Run Task: " + i);
                    Log.Info("TEST", "------------------ OnHandleWork Run Task: " + i);
                    //Toast.MakeText(Application.Context, "OnHandleWork Run Task: " + i, ToastLength.Short).Show();
                    i++;
                    await Task.Delay(2500);
                }
    
            });
    
    
            }
    

    CustomBroadcastReceiver:
    [BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
    class CustomBroadcastReceiver : BroadcastReceiver {
    public override void OnReceive(Context context, Intent intent) {
    Console.WriteLine("------------------ MiBroadcastReceiver");
    Log.Info("Fran", "------------------ MiBroadcastReceiver");
    Toast.MakeText(Application.Context, "MiBroadcastReceiver", ToastLength.Short).Show();

                if (intent.Action.Equals(Intent.ActionBootCompleted)) {
                    Console.WriteLine("------------------ MiBroadcastReceiver ActionBootCompleted");
                    Log.Info("TEST", "------------------ MiBroadcastReceiver ActionBootCompleted");
                    Toast.MakeText(Application.Context, "MiBroadcastReceiver ActionBootCompleted", ToastLength.Short).Show();
                }
    
                if (intent.Action.Equals(Intent.ActionPowerDisconnected)) {
                    Console.WriteLine("------------------ MiBroadcastReceiver ActionPowerDisconnected");
                    Log.Info("TEST", "------------------ MiBroadcastReceiver ActionPowerDisconnected");
                    Toast.MakeText(Application.Context, "MiBroadcastReceiver ActionPowerDisconnected", ToastLength.Short).Show();
                }
    
                if (intent.Action.Equals(Intent.ActionPowerConnected)) {
                    Console.WriteLine("------------------ MiBroadcastReceiver ActionPowerConnected");
                    Log.Info("TEST", "------------------ MiBroadcastReceiver ActionPowerConnected");
                    Toast.MakeText(Application.Context, "MiBroadcastReceiver ActionPowerConnected", ToastLength.Short).Show();
                }
    
            }
    
Sign In or Register to comment.