停止通知在应用程序关闭时被销毁

Stop Notification from being destroyed when the app closes

我正在制作一个 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();

感谢任何帮助,谢谢

到目前为止我知道的唯一方法是通过扩展 ServiceIntentService class

使用前台服务

并在您的 activity 或适配器中使用它来启动服务

context.startService(Intent(context,PersistentNotificationService.class))

这里的服务使用这个

public class PersistentNotificationService extends Service {

    private final static int ONE_SECOND = 1000;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notifyLemubit")
                .setSmallIcon(R.drawable.img_timer)
                .setContentTitle("Timer Running")
                .setContentText("Your title goes here")
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);


        new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {

            @Override
            public void onTick(long ms_until_done) {
                // Whatever code you want here
            }

            @Override
            public void onFinish() {
                // To cancel , just close the service
                stopForeground(true);
                stopSelf();
            }
        }.start();

        startForeground(2342, builder.build());

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}