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

标签 android android-studio notificationmanager

我正在尝试制作一个应用程序,当应用程序接收到的数据不正常时(假设 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/

相关文章:

android - 如何在android中压缩文件

java - 了解 Android 的代码 fragment IP 摄像机

android - Adobe AIR ADT Create Package 在 Windows 7 64 位上缺少图标

android-studio - 在jetpack compose的文本字段中限制小数点前后的位数

android.content.pm.PackageManager$NameNotFoundException : com. android.chrome

Android:状态栏中的多个通知作为单个列表

android - android中的经度和纬度为空

java - 如何在 Android Studio 中为静态导入修复 "Organize Imports"

android - 使用通知管理器将数据从一个 Activity 传递到另一个 Activity

android - StatusBarNotification 如何获取数据或重新发送 Intent ?