You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio库存系统中实现商品低库存、空库存、过期时的通知功能

Android库存管理系统三类场景自动通知实现方案

前置规则定义

先统一全局用到的预警参数和通知配置,可直接在项目常量类或者DatabaseHelp类中添加:

// 库存不足阈值,可根据业务调整
public static final int LOW_STOCK_THRESHOLD = 5;
// 通知渠道ID,Android 8.0及以上必填
public static final String STOCK_WARNING_CHANNEL_ID = "stock_warning";
// 通知ID
public static final int STOCK_WARNING_NOTIFICATION_ID = 1001;

1. 扩展DatabaseHelp数据库类,新增预警数据查询方法

在你现有SQLite操作类中添加查询符合预警条件商品的方法:

// 商品实体类可自行扩展字段,此处仅示例核心字段
public static class StockItem {
    public String name;
    public int stock;
    public long expireTime; // 过期时间存毫秒时间戳
    public int warningType; // 1=库存不足 2=库存为空 3=已过期
}

public List<StockItem> getWarningItems() {
    List<StockItem> warningList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    long currentTime = System.currentTimeMillis();
    // 查询满足三类预警条件的商品
    String querySql = "SELECT * FROM items WHERE stock = 0 OR stock < ? OR expire_time < ?";
    Cursor cursor = db.rawQuery(querySql, new String[]{String.valueOf(LOW_STOCK_THRESHOLD), String.valueOf(currentTime)});
    
    if (cursor.moveToFirst()) {
        do {
            StockItem item = new StockItem();
            item.name = cursor.getString(cursor.getColumnIndexOrThrow("item_name"));
            item.stock = cursor.getInt(cursor.getColumnIndexOrThrow("item_stock"));
            item.expireTime = cursor.getLong(cursor.getColumnIndexOrThrow("expire_time"));
            
            // 标记预警类型
            if (item.stock == 0) {
                item.warningType = 2;
            } else if (item.stock < LOW_STOCK_THRESHOLD) {
                item.warningType = 1;
            } else if (item.expireTime < currentTime) {
                item.warningType = 3;
            }
            warningList.add(item);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return warningList;
}

2. 封装通知发送工具方法

private void sendStockWarningNotification(Context context, List<StockItem> warningList) {
    if (warningList.isEmpty()) return;
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // Android 8.0及以上创建通知渠道
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                STOCK_WARNING_CHANNEL_ID,
                "库存预警通知",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel.setDescription("商品库存不足、空库存、过期预警提醒");
        channel.enableVibration(true);
        notificationManager.createNotificationChannel(channel);
    }
    
    // 统计各类型预警数量
    int lowStockCount = 0, emptyStockCount = 0, expireCount = 0;
    for (StockItem item : warningList) {
        if (item.warningType == 1) lowStockCount++;
        else if (item.warningType == 2) emptyStockCount++;
        else if (item.warningType == 3) expireCount++;
    }
    
    // 拼接通知内容
    String content = "";
    if (emptyStockCount > 0) content += emptyStockCount + "种商品库存为空 ";
    if (lowStockCount > 0) content += lowStockCount + "种商品库存不足 ";
    if (expireCount > 0) content += expireCount + "种商品已过期";
    
    // 构建通知,点击可跳转到预警列表页(PendingIntent可自行配置跳转逻辑)
    Notification notification = new NotificationCompat.Builder(context, STOCK_WARNING_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_warning) // 替换为你的通知图标
            .setContentTitle("库存预警提醒")
            .setContentText(content)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .build();
    
    notificationManager.notify(STOCK_WARNING_NOTIFICATION_ID, notification);
}

3. 选择预警触发时机

你可以根据业务需求选择以下任意一个或多个时机触发校验:

  • 每次App首页加载完成时,调用getWarningItems()查询并触发通知
  • 在AddItems类的商品新增/修改保存逻辑执行完成后,立刻触发一次校验,避免新操作的商品触发预警无提醒
  • 接入WorkManager实现后台定时校验,每天固定时间轮询一次数据库,防止用户长期不打开App漏收通知

AddItems类中触发示例

在你现有商品保存成功的逻辑后添加以下代码:

// 商品信息保存到数据库成功后
DatabaseHelp dbHelp = new DatabaseHelp(AddItems.this);
List<StockItem> warningItems = dbHelp.getWarningItems();
sendStockWarningNotification(AddItems.this, warningItems);

注意事项

  • Android 13及以上版本需要提前动态申请POST_NOTIFICATIONS权限,否则通知无法正常弹出
  • 库存不足阈值可根据业务需求调整,也可新增功能支持用户自定义阈值
  • 可给不同类型的预警配置不同的通知铃声、震动模式,区分紧急程度
  • 若需要查看预警商品明细,可配置通知的PendingIntent跳转到专门的预警列表页,展示所有预警商品信息

内容的提问来源于stack exchange,提问作者Noob Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 11:54:05