Android 有没有办法阻止我的应用在特定时间段内发送通知?
我正在尝试制作一个应用程序,当应用程序接收到的数据不是应该的数据时(想想蒸汽温度低于212华氏度),它会通知用户。如果用户在应用程序中,应用程序也会显示信息。在这种情况下,我可以随时收到通知。唯一的问题是信息必须是最新的,所以每10秒更新一次。如果数据持续不正确,应用程序将每隔10秒发送一次通知。有没有办法在指定时间内防止重复通知?(大约10-15分钟) 我曾尝试在Android 有没有办法阻止我的应用在特定时间段内发送通知?,android,android-studio,notificationmanager,Android,Android Studio,Notificationmanager,我正在尝试制作一个应用程序,当应用程序接收到的数据不是应该的数据时(想想蒸汽温度低于212华氏度),它会通知用户。如果用户在应用程序中,应用程序也会显示信息。在这种情况下,我可以随时收到通知。唯一的问题是信息必须是最新的,所以每10秒更新一次。如果数据持续不正确,应用程序将每隔10秒发送一次通知。有没有办法在指定时间内防止重复通知?(大约10-15分钟) 我曾尝试在for循环中使用thread.sleep(1000)使其等待10分钟,但这会使整个更新系统暂停10分钟,因此应用程序不会收到任何信息
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);
}
//错误通知
如果(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(上下文通知服务);
Notification notify=new Notification.Builder
(getApplicationContext())
.setContentTitle(“错误:设备错误”)
.setContentText(“有关详细信息,请参阅应用程序”)
.setSmallIcon(R.drawable.error\u notif)
.setSound(soundUri)
.build();
notify.flags |=notify.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,则返回out:
if(!YourClass.shouldSendNotification(context)){
return;
}
只需确保根据您的首选项(双关语)调整值即可。一个想法是,可以在首选项中节省通知时间,稍后当另一个通知到达时,您可以获得首选项的时间,并验证它们是否超过10分钟或您想要的时间:谢谢帮助!我想这会奏效的。我不熟悉android应用程序的开发,尤其是这种编码方式,因此我首先问了这个问题。因此,我需要一些帮助来排除故障。当我运行该程序时,会出现以下错误:“尝试在空对象引用上调用虚拟方法‘android.content.SharedReferences android.content.Context.GetSharedReferences(java.lang.String,int)’”我是否将它放在了错误的位置?或者我还需要做些什么?再次感谢您的帮助!每当您试图使用空对象(上下文)执行操作(GetSharedReferences())时,就会出现该错误。在本例中,您传递的变量上下文为null,这导致了问题。如果你想给我发信息,展示你正在使用的代码,或者只是想用你的代码创建一个链接,然后发布,我很乐意看一看。我想这就是你想要的。我也是GitHub的新手,所以如果您还有其他事情需要我做,请告诉我。该应用程序从Amazon的AWS cognito接收信息,因此它看起来可能有点奇怪。不用担心,您只需在第190行
setContentView(R.layout.activity_main)的正下方即可代码>添加这行代码:this.context=this.getApplicationContext()
应该可以解决您的问题。通常我们会使用“活动”上下文,但这是为了在“活动”可以关闭时触发的内容,在这种情况下,您可能希望它仍然会触发。@Jackson148捕捉正确!我在上面的代码中有一个逻辑错误;它已经被修复了。试试看它是否工作正常。