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

如何修改RemoteView中Chronometer的显示格式,仅展示分钟数?

解决方案:Android Widget 实现仅显示分钟数的“多久之前”字段

Chronometer 在 RemoteViews 中存在局限性:它的 format 参数只能将默认计时字符串(如 MM:SS 或 HH:MM:SS)替换到 %s 位置,无法直接提取并仅展示分钟数值。以下是两种可行的替代方案:


方案一:TextView + WorkManager 周期性更新(推荐)

这是实现准确分钟级“多久之前”显示的可靠方案,虽然需要使用 WorkManager,但能满足你对显示精度的需求。

步骤1:替换布局中的 Chronometer 为 TextView

<TextView
    android:id="@+id/text_ago"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/img_bg"
    android:layout_marginStart="40dp"
    android:layout_marginBottom="19dp" />

步骤2:创建 WorkManager Worker 类

用于每分钟计算并更新分钟数:

class UpdateWidgetWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {
        val appWidgetManager = AppWidgetManager.getInstance(applicationContext)
        val widgetComponent = ComponentName(applicationContext, HomeWidgetProvider::class.java)
        val widgetIds = appWidgetManager.getAppWidgetIds(widgetComponent)

        widgetIds.forEach { widgetId ->
            // 替换为你的事件发生时间(建议从SharedPreferences或本地存储读取)
            val eventTimestamp = System.currentTimeMillis() - 50 * 1000
            // 计算分钟差(向下取整,可根据需求改为向上取整)
            val minutesAgo = ((System.currentTimeMillis() - eventTimestamp) / (1000 * 60)).toInt()
            
            val views = RemoteViews(applicationContext.packageName, R.layout.home_widget)
            views.setTextViewText(R.id.text_ago, "$minutesAgo minutes ago")
            appWidgetManager.updateAppWidget(widgetId, views)
        }
        return Result.success()
    }
}

步骤3:在 WidgetProvider 中启动周期性任务

class HomeWidgetProvider : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        // 启动每分钟一次的更新任务
        val workRequest = PeriodicWorkRequestBuilder<UpdateWidgetWorker>(1, TimeUnit.MINUTES)
            .build()
        WorkManager.getInstance(context)
            .enqueueUniquePeriodicWork("WidgetUpdate", ExistingPeriodicWorkPolicy.REPLACE, workRequest)

        // 首次初始化更新
        appWidgetIds.forEach { updateHomeWidget(context, appWidgetManager, it) }
    }

    private fun updateHomeWidget(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetId: Int
    ) {
        val views = RemoteViews(context.packageName, R.layout.home_widget)
        val eventTimestamp = System.currentTimeMillis() - 50 * 1000
        val minutesAgo = ((System.currentTimeMillis() - eventTimestamp) / (1000 * 60)).toInt()
        views.setTextViewText(R.id.text_ago, "$minutesAgo minutes ago")
        views.setTextViewText(R.id.text_message, "Testing...")
        
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }
}

方案二:近似处理(仅适用于接受模糊显示的场景)

如果可以接受模糊的分钟数展示(比如将 00:50 显示为 00 minutes ago),可以保留 Chronometer,但无法实现精确的分钟数提取:

<Chronometer
    android:id="@+id/text_ago"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/img_bg"
    android:layout_marginStart="40dp"
    android:layout_marginBottom="19dp"
    android:format="%s ago" />

这种方式本质还是显示完整的 MM:SS 格式,只是附加了“ago”后缀,无法单独提取分钟数。


内容的提问来源于stack exchange,提问作者M.SH

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 20:52:13