android - 仅在达到目标后显示一次通知
问题描述
当用户达到目标(步行 84 公里)时,我正在显示通知。我正在显示此通知,但是当我重新打开应用程序时,它再次显示。我怎样才能让它只运行一次(一旦用户到达84公里)?
这是我正在检查 KM 的代码(在asynctask中)
protected String doInBackground(String... aurl) {
totalKm = (int) Float.parseFloat(TripsInfo.km.get(TripsInfo.userRank - 1));
if (totalKm == 84 ) {
milestoneNotification(totalKm);
} else if (totalKm == 130 ) {
milestoneNotification(totalKm);
} else if (totalKm == 200) {
milestoneNotification(totalKm);
}
return null;
}
和另一个创建通知的功能
public void milestoneNotification(int km_up) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(Profile.this, CHANNEL_ID)
.setSmallIcon(R.mipmap.icon_app)
.setContentTitle("Gekon")
.setContentText("Congratulations!You reached " + km_up + "KM!")
.setVibrate((new long[]{1000, 1000, 1000, 1000, 1000}))
.setLights(Color.GREEN, 3000, 3000)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, mBuilder.build());
}
解决方案
推荐阅读
- node.js - 雅虎天气 API 抛出“forEach”错误
- html - 有没有办法根据浏览器的高度和宽度设置 BS4 轮播图像的高度和宽度?
- javascript - 同时写入两个数据库引用
- c# - 当属性是不同的对象类型时将代码合并到基类中
- python - 在 ElementTree (Python) 中为 SubElement 添加值
- c# - Xamarin.Mac - 通过从下拉列表中选择选项对 TableView 进行排序
- android - 具有列表和嵌套对象的 ViewModel 对象
- php - PHP:这个库如何拦截 curl 函数?
- bash - 为大文件优化 awk 脚本
- php - 通过 PHP 中的电子邮件正文发送带有变量的 url