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

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

Stop Notification from being destroyed when the app closes

问题

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

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

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

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();

感谢任何帮助。

英文:

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:

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();

Any help is appreciated, Thanks

答案1

得分: 1

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

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

context.startService(new 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) {
                // 任何您想要的代码
            }

            @Override
            public void onFinish() {
                // 要取消,只需关闭服务
                stopForeground(true);
                stopSelf();
            }
        }.start();

        startForeground(2342, builder.build());

        return START_STICKY;
    }

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

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

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

For the service here use this one

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

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:

确定