阻止通知在应用关闭时被销毁。

huangapple go评论122阅读模式
英文:

Stop Notification from being destroyed when the app closes

问题

我正在制作一个安卓应用程序,允许用户按下按钮,显示一个带有倒计时定时器的通知,倒计时一定时间。尽管我已经让通知变为持久化,以便无法被关闭,但当应用程序关闭时,通知会被销毁。

是否有任何方法可以让通知在应用程序关闭后继续运行而不被销毁。

以下是启动通知和定时器的代码:

  1. final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
  2. .setSmallIcon(holder.img_timer.getImageAlpha())
  3. .setContentTitle("Timer Running")
  4. .setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + timer.getTimer_duration_s())
  5. .setOngoing(true)
  6. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  7. final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
  8. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  9. new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
  10. @Override
  11. public void onTick(long ms_until_done) {
  12. builder.setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + ms_until_done / ONE_SECOND);
  13. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  14. }
  15. @Override
  16. public void onFinish() {
  17. notificationManagerCompat.cancel(timer.getTimer_id());
  18. final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
  19. .setSmallIcon(holder.img_timer.getImageAlpha())
  20. .setContentTitle("Timer Finished")
  21. .setContentText("Your " + timer.getTimer_name() + " is Fully Grown!")
  22. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  23. final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
  24. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  25. }
  26. }.start();

感谢任何帮助。

英文:

I am making a android app which will allow the user to press a button and show a notification with a timer counting down for a certain amount of time. Although I have made the notification persistent so it cannot be dismissed, when the app closes the notification gets destroyed.

Is there any way to allow a notification to continue running once the app is closed and not get destroyed.

Here is the code for starting my notification and timer:

  1. final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
  2. .setSmallIcon(holder.img_timer.getImageAlpha())
  3. .setContentTitle("Timer Running")
  4. .setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + timer.getTimer_duration_s())
  5. .setOngoing(true)
  6. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  7. final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
  8. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  9. new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
  10. @Override
  11. public void onTick(long ms_until_done) {
  12. builder.setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + ms_until_done / ONE_SECOND);
  13. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  14. }
  15. @Override
  16. public void onFinish() {
  17. notificationManagerCompat.cancel(timer.getTimer_id());
  18. final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
  19. .setSmallIcon(holder.img_timer.getImageAlpha())
  20. .setContentTitle("Timer Finished")
  21. .setContentText("Your " + timer.getTimer_name() + " is Fully Grown!")
  22. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  23. final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
  24. notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
  25. }
  26. }.start();

Any help is appreciated, Thanks

答案1

得分: 1

我唯一目前知道的方法是通过扩展 ServiceIntentService 类来使用前台服务

在您的活动或适配器中,可以使用以下代码来启动服务

  1. context.startService(new Intent(context, PersistentNotificationService.class));

对于此服务,请使用以下代码

  1. public class PersistentNotificationService extends Service {
  2. private final static int ONE_SECOND = 1000;
  3. @Override
  4. public int onStartCommand(Intent intent, int flags, int startId) {
  5. final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notifyLemubit")
  6. .setSmallIcon(R.drawable.img_timer)
  7. .setContentTitle("Timer Running")
  8. .setContentText("Your title goes here")
  9. .setOngoing(true)
  10. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  11. new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
  12. @Override
  13. public void onTick(long ms_until_done) {
  14. // 任何您想要的代码
  15. }
  16. @Override
  17. public void onFinish() {
  18. // 要取消,只需关闭服务
  19. stopForeground(true);
  20. stopSelf();
  21. }
  22. }.start();
  23. startForeground(2342, builder.build());
  24. return START_STICKY;
  25. }
  26. @Nullable
  27. @Override
  28. public IBinder onBind(Intent intent) {
  29. return null;
  30. }
  31. }
英文:

The only way by far i know is using a Foreground service by extending the Service or IntentService class

And inside your activity or adapter use this to start the service

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

For the service here use this one

  1. public class PersistentNotificationService extends Service {
  2. private final static int ONE_SECOND = 1000;
  3. @Override
  4. public int onStartCommand(Intent intent, int flags, int startId) {
  5. final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notifyLemubit")
  6. .setSmallIcon(R.drawable.img_timer)
  7. .setContentTitle("Timer Running")
  8. .setContentText("Your title goes here")
  9. .setOngoing(true)
  10. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  11. new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
  12. @Override
  13. public void onTick(long ms_until_done) {
  14. // Whatever code you want here
  15. }
  16. @Override
  17. public void onFinish() {
  18. // To cancel , just close the service
  19. stopForeground(true);
  20. stopSelf();
  21. }
  22. }.start();
  23. startForeground(2342, builder.build());
  24. return START_STICKY;
  25. }
  26. @Nullable
  27. @Override
  28. public IBinder onBind(Intent intent) {
  29. return null;
  30. }
  31. }

huangapple
  • 本文由 发表于 2020年9月6日 17:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63762687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定