CountDownTimer 在应用从最近刷屏后没有 运行 服务?
CountDownTimer does not run in service after app is swiped from recents?
我正在尝试显示一个通知以倒计时秒数,直到我的 CountDownTimer 在服务中结束 - 并且工作正常,直到我将我的应用程序从最近菜单中滑开。然后它会卡在它打开的那一秒钟。
我正在使用 startForeground()
。
我究竟做错了什么?在我的 OnePlus 5t 和 Google 像素模拟器上进行了测试。
public class PersistentNotificationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showPersistentNotification();
return Service.START_STICKY;
}
void showPersistentNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "persistentNotification");
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setColor(Color.parseColor("#0097A7"))
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setVibrate(new long[0])
.setContentTitle("Time until period ends");
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
startForeground(0, builder.build());
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
builder.setContentText(millisUntilFinished / 1000 + " seconds until period ends");
notificationManager.notify(1, builder.build());
}
public void onFinish() {
showPersistentNotification();
}
}.start();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
好吧,毕竟,唯一的问题是我在调用 startForeground
.
时需要传递一个大于零的 ID
更改为:startForeground(0, builder.build());
为:startForeground(1, builder.build());
我正在尝试显示一个通知以倒计时秒数,直到我的 CountDownTimer 在服务中结束 - 并且工作正常,直到我将我的应用程序从最近菜单中滑开。然后它会卡在它打开的那一秒钟。
我正在使用 startForeground()
。
我究竟做错了什么?在我的 OnePlus 5t 和 Google 像素模拟器上进行了测试。
public class PersistentNotificationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showPersistentNotification();
return Service.START_STICKY;
}
void showPersistentNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "persistentNotification");
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setColor(Color.parseColor("#0097A7"))
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setVibrate(new long[0])
.setContentTitle("Time until period ends");
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
startForeground(0, builder.build());
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
builder.setContentText(millisUntilFinished / 1000 + " seconds until period ends");
notificationManager.notify(1, builder.build());
}
public void onFinish() {
showPersistentNotification();
}
}.start();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
好吧,毕竟,唯一的问题是我在调用 startForeground
.
更改为:startForeground(0, builder.build());
为:startForeground(1, builder.build());