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

snz8szmq  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(524)

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

谢谢你的帮助

az31mfrm

az31mfrm1#

到目前为止,我知道的唯一方法是通过扩展 Service 或者 IntentService
在活动或适配器中,使用此命令启动服务

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;
    }
}

相关问题