Search code examples
androidbackground-service

Start App From Service in BackGround in oreo and newer


my goal is show an activity in every where(even lock screen) on a specific time.

i could in previous android versions until Oreo do this work , but after Oreo Google change the methods, "if you want to display a view on lockScreen use "Activity.this.setShowWhenLocked(true);" :Google says. then i have to create an activity and only use a service wasn't enough.

My Solution : i use AlarmManger to run a service in background on a specific time, that service start my activity(my goal).

Problem : in previous versions android until Oreo , i don't have problem. app works fine . even that time i close my app from recent app , or phone locked. but in Oreo and newer when app works when it is in foreground.

point : in my intent i use this flag :FLAG_ACTIVITY_NEW_TASK

question : how to run a specific activity from a background service even when app is close or phone locked, in Oreo and newer version?


Solution

  • in addition that works i done previous, i understood MIUI in Xiaomi prevent from displaying pop-up window that comes from back ground , then i got its permission and solved problem, now i can see my view anywhere i want. here is a brief for whom that want :

    Permissins :

    1.Overlay window

    2.pop-up window from background(in Xiaomi and probably Other Chinese Phone UI)

    Levels :

    1. set a alarm from Activity with Calendar & PendingIntent in AlarmManger to run my Service in a specific time

    2. when i computed my values in Service then Intent to ActivityViewer, in this step i add FLAG_ACTIVITY_NEW_TASK to my intent(according to google docs in android Pie you have to add this flag to your intent for this usage).

    3. In my ActivityViewer according to google docs , I use Activity.setWhenLock() method to show my view even in lock screen and use TYPE_APPLICATION_OVERLAY for Oreo and newer , and use TYPE_TOAST for version 6 until Oreo , and use TYPE_SYSTEM_ERROR for before 6 , in setLayoutParams for setType.

    I use this code for getting pop-up window from background permissin in Xiaomi :

    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter",
                    "com.miui.permcenter.permissions.PermissionsEditorActivity");
            intent.putExtra("extra_pkgname", getPackageName());
            startActivity(intent);