[英]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.