【发布时间】:2017-01-04 19:40:25
【问题描述】:
我知道这个问题被问了很多次,但即使我的应用程序被杀死,我也没有找到任何解决方案来保持服务活着。 我的应用程序在所有设备上运行,但 某些设备 如果我终止了该应用程序,那么我的服务也会终止 (设备名称 MI 4 版本 ans asus 5.0.3)
以下是我启动前台服务的服务代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("My service started at ")
.setTicker("My service started")
.setContentText("app")
.setSmallIcon(R.drawable.app)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true).build();
startForeground(Constants.FOREGROUND_SERVICE,notification);
}
【问题讨论】:
-
这是 MIUI,放松。它的工作方式与 Android 不同。
-
@VladMatvienko 有什么解决办法吗
-
我个人认为这不是我(也不是你)的问题。我们不是那些应该为那些奇怪而愚蠢的 MIUI 变化而受苦的人。我能给出的唯一建议是每隔 5 秒检查一次由 AlarmManager 运行的服务,如果它关闭,则启动它。
-
@VladMatvienko 谢谢你的回答
-
@VladMatvienko 我的 AlarmManager 也被杀死了