英文:
How can I set a notification on a specific day and specific time?
问题
我想在特定日期的特定时间显示通知,就像我在标题中写的那样。
现在,我的提醒在按下按钮后的10秒内出现。如果我需要设置特定日期,我需要这样做吗?我需要将日期转换为毫秒,并提供这个参数吗?
以下是我的代码:
package com.example.timeman;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class ReminderBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notify")
.setSmallIcon(R.drawable.ic_baseline_close_24)
.setContentTitle("Salamabuliy")
.setContentText("TextNotif")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(200, builder.build());
}
}
还有点击方法:
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
long timeAtButtonClick = System.currentTimeMillis();
long tenSecondsInMillis = 1000 * 10;
alarmManager.set(AlarmManager.RTC, timeAtButtonClick + tenSecondsInMillis, pendingIntent);
//displayNotification();
OpenThis(view);
}
英文:
I want to make a notification to appear on a specific date at a specific time, as I wrote in the title.
Now my alerts come in 10 seconds after pressing the button. If I need to set a specific date, do I need to do that as well? I need convert the date into milliseconds and give this parameter?
Here's my code:
package com.example.timeman;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class ReminderBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notify")
.setSmallIcon(R.drawable.ic_baseline_close_24)
.setContentTitle("Salamabuliy")
.setContentText("TextNotif")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(200, builder.build());
}
}
And OnClick Method:
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
long timeAtButtonClick = System.currentTimeMillis();
long tenSecondsInMillis = 1000*10;
alarmManager.set(AlarmManager.RTC, timeAtButtonClick + tenSecondsInMillis, pendingIntent);
//displayNotification();
OpenThis(view);
}
答案1
得分: 4
一个选择是找到从现在到您希望其显示的日期之间的毫秒持续时间。例如;
LocalDateTime now = LocalDateTime.now();
LocalDateTime end = now.plusSeconds(10); // 结束日期可以是具体的,这只是一个示例
Duration difference = Duration.between(now, end);
long differenceMillis = difference.toMillis();
英文:
One option would be to find the duration in millis between now and the date you want it to appear on. For example;
LocalDateTime now = LocalDateTime.now();
LocalDateTime end = now.plusSeconds(10); // end date can be specific, this is an example
Duration difference = Duration.between(now, end);
long differenceMillis = difference.toMillis();
答案2
得分: 0
使用 Instant#toEpochMilli()
和 Instant#plusSeconds
,如下所示:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.toEpochMilli());
// After 10 seconds
Instant after10SecFromNow = now.plusSeconds(10);
long millisAfter10Sec = after10SecFromNow.toEpochMilli();
System.out.println(millisAfter10Sec);
// alarmManager.set(AlarmManager.RTC, millisAfter10Sec, pendingIntent);
}
}
输出:
1597837345043
1597837355043
了解更多关于 java.time
<sup>*</sup> 的内容,可以参考 现代日期时间 API,以及 教程:日期时间。
<sup>* 如果由于某种原因,你必须使用 Java 6 或 Java 7,你可以使用 ThreeTen-Backport 来将大部分 java.time 功能回溯到 Java 6 和 7。如果你正在进行一个 Android 项目,并且你的 Android API 级别仍然不符合 Java 8,可以查看 通过 desugaring 使用 Java 8+ API 以及 如何在 Android 项目中使用 ThreeTenABP。
</sup>
英文:
Use Instant#toEpochMilli()
and Instant#plusSeconds
as shown below:
import java.time.Instant;
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.toEpochMilli());
// After 10 seconds
Instant after10SecFromNow = now.plusSeconds(10);
long millisAfter10Sec = after10SecFromNow.toEpochMilli();
System.out.println(millisAfter10Sec);
// alarmManager.set(AlarmManager.RTC, millisAfter10Sec, pendingIntent);
}
}
Output:
1597837345043
1597837355043
Learn more about java.time
<sup>*</sup>, the modern Date-Time API, from Trail: Date Time.
<sup>* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论