问题
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 :
set a
alarmfromActivitywithCalendar&PendingIntentinAlarmMangerto run myServicein a specific timewhen i computed my values in
ServicethenIntenttoActivityViewer, in this step i addFLAG_ACTIVITY_NEW_TASKto my intent(according to google docs inandroidPieyou have to add this flag to your intent for this usage).In my
ActivityVieweraccording to google docs , I useActivity.setWhenLock()method to show my view even in lock screen and useTYPE_APPLICATION_OVERLAYfor Oreo and newer , and useTYPE_TOASTfor version 6 until Oreo , and useTYPE_SYSTEM_ERRORfor before 6 , insetLayoutParamsforsetType.
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