Start App From Service in BackGround in oreo and newer

こ雲淡風輕ζ 提交于 2020-02-05 04:28:28

问题


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?


回答1:


You can't achieve this desired result using background service instead you must consider Foreground service for this task. Here you can find further details about Foreground Services in Android. docs reference

Hope this will help you.




回答2:


Pardon, but the question here is how to run an activity or a task even when the app is closed. Foreground service will exist as long as the user is interacting with the app or there is a persistent notification showing the task progress, this isn't a case here.

I'd strongly recommend you to go for the workmanager to perform background task. There is a brilliant codelab on the same here :

https://codelabs.developers.google.com/codelabs/android-workmanager/#0

and read the docs here :

https://developer.android.com/topic/libraries/architecture/workmanager




回答3:


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);


来源:https://stackoverflow.com/questions/59745445/start-app-from-service-in-background-in-oreo-and-newer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!