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

如何检测应用上次启动时间间隔并实现超14天活跃提示?

检测应用上次启动时间间隔并触发活跃度提示的方案

嗨,我来给你拆解一下这个问题的解决方案,核心思路其实很简单:每次启动应用时把当前时间存在本地,下次启动时取出上次的时间戳算间隔,达到14天就弹出提示。下面分通用逻辑和各平台具体实现来聊:

一、通用核心步骤

不管你用什么平台,都逃不开这几步:

  • 应用启动第一时间,读取本地存储里的「上次启动时间戳」
  • 获取当前时间的时间戳(统一用毫秒或秒,别搞混)
  • 计算两个时间戳的差值,转换成天数(一天=86400秒=86400000毫秒)
  • 如果差值≥14天,就弹出"应提升活跃度"的提示;同时一定要更新本地存储的时间戳为当前时间
  • 如果差值小于14天,直接更新本地存储的时间戳就行(保证每次启动都记录最新时间)

二、各主流平台的具体代码实现

1. Android 平台(用SharedPreferences)

直接用系统自带的SharedPreferences存时间戳,代码示例(Kotlin):

// 在Application的onCreate或者主Activity的onCreate里执行
val appUsagePref = getSharedPreferences("AppUsageRecord", Context.MODE_PRIVATE)
val lastLaunchMs = appUsagePref.getLong("last_launch_ms", 0L)
val currentMs = System.currentTimeMillis()

// 计算天数差
val daysGap = (currentMs - lastLaunchMs) / 86400000

if (daysGap >= 14) {
    // 这里可以换成你自己的提示样式,比如Dialog或者Snackbar
    Toast.makeText(this, "应提升活跃度", Toast.LENGTH_LONG).show()
}

// 别忘了更新最新的启动时间
appUsagePref.edit().putLong("last_launch_ms", currentMs).apply()

2. iOS 平台(用UserDefaults)

iOS里用UserDefaults来存,代码示例(Swift):

// 在AppDelegate的application(_:didFinishLaunchingWithOptions:)或者SceneDelegate的scene(_:willConnectTo:options:)里执行
let defaults = UserDefaults.standard
let lastLaunchTime = defaults.double(forKey: "last_launch_timestamp")
let currentTime = Date().timeIntervalSince1970

// 计算天数差
let daysGap = (currentTime - lastLaunchTime) / 86400

if daysGap >= 14 {
    // 弹出提示框
    let alert = UIAlertController(title: "温馨提示", message: "应提升活跃度", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "知道了", style: .default))
    
    // 获取根控制器来弹框,根据你的项目结构调整
    if let rootVC = UIApplication.shared.connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .first?.rootViewController {
        rootVC.present(alert, animated: true)
    }
}

// 更新存储的时间戳
defaults.set(currentTime, forKey: "last_launch_timestamp")
defaults.synchronize()

3. 跨平台框架(Flutter,用shared_preferences插件)

先在pubspec.yaml里引入shared_preferences,然后写逻辑:

import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';

// 可以在main函数里或者首页initState里调用这个方法
Future<void> checkLaunchInterval() async {
  final prefs = await SharedPreferences.getInstance();
  final lastLaunchMs = prefs.getInt('last_launch_ms') ?? 0;
  final currentMs = DateTime.now().millisecondsSinceEpoch;

  final daysGap = (currentMs - lastLaunchMs) ~/ (1000 * 60 * 60 * 24);

  if (daysGap >= 14) {
    // 弹出提示对话框
    if (context != null) {
      showDialog(
        context: context!,
        builder: (ctx) => AlertDialog(
          title: const Text('提示'),
          content: const Text('应提升活跃度'),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(ctx),
              child: const Text('知道了'),
            ),
          ],
        ),
      );
    }
  }

  // 更新最新启动时间
  await prefs.setInt('last_launch_ms', currentMs);
}

三、几个要注意的细节

  • 首次启动处理:第一次打开应用时,本地没有存储的时间戳,默认值是0,这时候计算出来的天数差会非常大。如果你不想首次启动就弹提示,可以加个判断:if (lastLaunchMs != 0 && daysGap >=14)
  • 时间单位统一:存储和计算时一定要用相同的时间单位(要么都用毫秒,要么都用秒),不然会算出错误的天数差
  • 数据持久化:用系统自带的本地存储方案就够了,除非你需要跨设备同步,那才考虑云端存储。本地存储的话,只要用户不卸载应用,数据就会一直保留

内容的提问来源于stack exchange,提问作者Jia Chen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:34:06