gpt4 book ai didi

java - Android中如何在一定时间后显示通知

转载 作者:行者123 更新时间:2023-12-02 10:20:24 24 4
gpt4 key购买 nike

我正在开发一个基于特定通知的应用程序,用户通过该通知执行我想要显示通知的操作,并且在通知后我想添加添加时间 + 5 分钟来显示下一个通知。并在应用程序关闭时在后台运行计时器进程。

 public long Time = 3600;
private NotificationManagerCompat notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = NotificationManagerCompat.from(this);
Timer();

}


public void Timer() {

for (long i = Time; i < 86400000; i = i += 60000) {

new CountDownTimer(1000, i) {
@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {
ShowNotification();


}
}.start();
}


}

public void ShowNotification() {
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setContentTitle("Example")
.setContentText("Example Action")
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.build();

notificationManager.notify(1, notification);


}

最佳答案

在这种情况下尝试 AlarmReceiver。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 2);
Intent intent = new Intent("android.action.DISPLAY_NOTIFICATION");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

关于java - Android中如何在一定时间后显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54381645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com