gpt4 book ai didi

android - 有没有办法阻止我的应用程序在特定时间范围内发送通知?

转载 作者:行者123 更新时间:2023-11-29 00:52:41 26 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,当应用程序接收到的数据不正常时(假设 Steam 温度低于 212 华氏度),它会通知用户。如果用户在应用程序中,该应用程序还会显示信息。只要是这种情况,我就能够收到要发送的通知。唯一的问题是信息需要实时更新,因此每 10 秒更新一次。如果数据持续不正确,这会使应用程序每 10 秒发送一次通知。有没有办法在指定时间内防止重复通知? (大约 10 - 15 分钟)

我尝试在 for 循环中使用 thread.sleep(1000) 让它等待 10 分钟,但这会使整个更新系统暂停 10 分钟,所以没有信息会进入应用程序。我是 android studio 的新手,在网上找不到任何可以帮助我解决这个问题的东西。

这就是应用知道发送通知的方式。如果我可以继续使用它,那将是理想的,但如果有更好的方法,我愿意改变它。

 //ERROR Notification
if (map.get("steamTemp") < 212 || map.get("steamTemp") > 500 ||
map.get("waterTemp") < 40 || map.get("waterTemp") > 150||
map.get("dieselFlow") < 50 || map.get("dieselFlow") > 100 ||
map.get("waterFlow") < 50 || map.get("waterFlow") > 100||
map.get("waterFeederLevel") < 10 || map.get("waterFeederLevel") > 150) {
NotificationManager notif = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification.Builder

(getApplicationContext())
.setContentTitle("ERROR: Device Error")
.setContentText("Please see app for more information.")
.setSmallIcon(R.drawable.error_notif)
.setSound(soundUri)
.build();

notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);

}

我希望在出现问题时发送通知,但之后要等待 10 分钟再发送另一个通知。我上面解释的 thread.sleep(1000) 示例的问题是它暂停了整个应用程序,而不仅仅是通知。这是不行的,因为应用需要在用户查看时显示更新的信息。

最佳答案

正如 carlosgub 提到的,您可以使用自己硬编码或用户输入的首选项来确定何时发送它们。这是一些示例代码:

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Date;

public class YourClass {

//Key in Shared Preferences to use
private static final String LAST_UPDATE = "lastUpdate";
//10 minutes, adjust to your preferences
private static final long NUMBER_MILLISECONDS_BETWEEN_UPDATES = (1000L * 60L * 10L);
//Your Shared Preferences name
private static final String SP_NAME = "yourSPName";

private static void saveLastUpdateTime(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
long x = new Date().getTime();
editor.putLong(LAST_UPDATE, x);
editor.commit();
}

private static long getLastUpdateTime(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sharedPref.getLong(LAST_UPDATE, -1);
}

public static boolean shouldSendNotification(Context context) {
long lastUpdate = getLastUpdateTime(context);
if (lastUpdate == -1) {
saveLastUpdateTime(context);
//This means it has yet to run
return true;
}
long now = new Date().getTime();
if ((now - lastUpdate) > NUMBER_MILLISECONDS_BETWEEN_UPDATES) {
saveLastUpdateTime(context);
return true;
} else {
return false;
}
}
}

在这里的代码中,你调用了shouldSendNotification(),如果返回false,说明距离上次运行还不到10分钟,如果返回true,说明已经超过10分钟了自上次运行(或根本未运行)以来的分钟数。

要在您的代码中使用它,在构建notify 对象的方法顶部调用它,如果它返回 false,则返回:

if(!YourClass.shouldSendNotification(context)){
return;
}

只需确保根据您的喜好调整值(双关语)。

关于android - 有没有办法阻止我的应用程序在特定时间范围内发送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57828199/

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