我正在做一个android应用程序,它允许用户按下一个按钮,并显示一个计时器倒计时一定时间的通知。虽然我已经使通知持久化,所以它不能被驳回,但当应用程序关闭时,通知会被销毁。
有没有办法让通知在应用程序关闭后继续运行而不会被销毁。
以下是启动通知和计时器的代码:
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
.setSmallIcon(holder.img_timer.getImageAlpha())
.setContentTitle("Timer Running")
.setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + timer.getTimer_duration_s())
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
@Override
public void onTick(long ms_until_done) {
builder.setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + ms_until_done / ONE_SECOND);
notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
}
@Override
public void onFinish() {
notificationManagerCompat.cancel(timer.getTimer_id());
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
.setSmallIcon(holder.img_timer.getImageAlpha())
.setContentTitle("Timer Finished")
.setContentText("Your " + timer.getTimer_name() + " is Fully Grown!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
}
}.start();
谢谢你的帮助
1条答案
按热度按时间az31mfrm1#
到目前为止,我知道的唯一方法是通过扩展
Service
或者IntentService
班在活动或适配器中,使用此命令启动服务
这里的服务用这个