Search code examples
c#xamarinxamarin.formsxamarin.android

Xamarin android relaunch completely closed app at specific time - Android Version 10.0


I implement this functionality at previous Android versions and its work without any problem until version 10

This is my code:

[BroadcastReceiver(Enabled = true, Exported = false)]
    public class Alarm : BroadcastReceiver
    {
        public async override void OnReceive(Context context, Intent intent)
        {
           
            PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
            PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "");
            wl.Acquire();

            Vibrator v = (Vibrator)context.GetSystemService(Context.VibratorService);
            v.Vibrate(800);

            var audio = new AudioService();
            await audio.PlayMp3File("1");
                                    
            Intent i = new Intent(context, typeof(MainActivity));
            i.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(i);
        }

    }

At version 10.0 the device play the MP3 Audio without launch the application

I could not find solution for this but I quote this from Wikipedia

Privacy and security

Several major security and privacy changes are present in Android 10: ........... There are also new restrictions on the launching of activities by background apps.For security (due to its use by clickjacking malware) and performance reasons, Android 10 Go Edition forbids use of overlays, except for apps that received the permission before a device was upgraded to Android 10.

Is there any solution or it dismissed ?


Solution

  • It is now recommended by Google to use a notification as a prompt to launch your application from the "background".

    i.e. The user must be involved the decision to launch an application

    In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity. Examples of when to use such notifications include handling an incoming phone call or an active alarm clock.

    Google has a complete doc that covers when & how Android10 handles it now: