简体   繁体   English

如何在杀死后台服务的设备(例如Xiomi)上在后台运行应用程序?

[英]how to run app in background on devices that kill background services (like Xiomi)?

I have tried running a service in background. 我尝试在后台运行服务。 Have used Start_Sticky. 已使用Start_Sticky。 Basically this answer . 基本上这个答案 It works in many devices fine but in some devices like Xiaomi Lenovo the service dies when app is removed from "recent app" screen. 它可以在许多设备上正常运行,但在某些设备(例如小米联想)中,当应用程序从“最近的应用程序”屏幕中删除时,该服务就会消失。 I have also tried deactivating power saver for particular apps and full system but that doesn't work either. 我也曾尝试停用特定应用程序和整个系统的节电功能,但这也不起作用。

How do I make it work in those devices? 如何在这些设备上使用它? I know its possible because some apps (like whatsapp) and games are able to send notification even when the app is not in "recent app" screen. 我知道这是可能的,因为某些应用程序(例如whatsapp)和游戏即使不在“最近的应用程序”屏幕中也能够发送通知。

You can create the alarm with 1min in the onTaskRemoved() method in your service class. 您可以在服务类的onTaskRemoved()方法中以1分钟的时间创建警报。 It will automatically invoke after the 1min and restarts the service. 1分钟后它将自动调用并重新启动服务。

In service class 在职舱

public void onTaskRemoved(Intent rootIntent) {
     Intent restartService = new Intent(getApplicationContext(), YourService.class);
     restartService.setPackage(Yourpackagename);
     PendingIntent restartServiceIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
     AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
     alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealTime()+1000, restartServiceIntent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在oppo-coloros,vivo-funtouch os,Xiomi-MIUI操作系统等定制操作系统中保护后续服务/警报被杀死? - How to protect background service/alarms to be kill in newly launched devices in customized OS like oppo - coloros, vivo-funtouch os, Xiomi-MIUI os? 使用后台服务终止应用 - Kill an App with background Services 如何以编程方式杀死三星设备上的后台进程 - How to kill background processes on Samsung devices programmatically 即使应用程序在MI设备中被杀死,如何运行后台服务 - How to run a background service even the app is killed in MI devices 如何监视Android设备中应用程序的后台服务? - How to monitor background services for apps in Android devices? 在Oreo(8.0.0+)(API 26+)中,如何在应用程序处于后台或杀死时获取位置服务更新 - In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill 如何终止Android应用后台进程? - How to kill an Android app background process? 如何在后台运行应用 - How to run an app in background 如何在后台连续运行服务类即使应用程序崩溃(将应用程序杀死) - How to run service class in background continuously Even app crash (swipe the app to kill) Robotium如何使用后台服务(例如转换)进行测试? - Robotium How to test with background Services like conversion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM