【发布时间】:2020-03-06 21:33:53
【问题描述】:
我的应用程序执行非常简单的任务 - 它收集 cellInfo,提取蜂窝塔的 cid 编号,并在存在带有某种单元 id 日志的文本视图时将其置于主要活动中。每个cellid每2秒收集一次。此功能存储在 Intent Service 中,并且每次用户开始登录时都会出现通知。
最近我发现,尽管在 do-while 循环中有一个Thread.sleep(1000L),但某些智能手机并不能保持服务正常工作。 IE。当我关闭屏幕时,意图服务停止工作,但通知仍然存在。有时我注意到有些手机运行的功能是让 cellids 更慢而忽略睡眠方法。但是当我启动一个插入充电器的应用程序时,每部手机都可以正常工作。不确定这一切都与充电有关,但我需要延迟在多部手机上相同。
这是我尝试过的。
在不同的手机上以不同的速度运行
fun counter(bc: Intent){
val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
var quit = 0
var i = 0
do {
i += 1
quit = Trigger.getTrigger() // companion object
val num_cid = getcids(tm) // function that takes cell tower cid (int)
bc.putExtra(PARAM_OUT_MSG, num_cid .toString()) // sending cid to broadcastIntent
sendBroadcast(bc)
Thread.sleep(1000)
}
while (quit == 0)
}
效果更好,但没有出现通知。
fun counter(bc: Intent){
val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Handler().postDelayed(object : Runnable {
override fun run() {
if (Trigger.getTrigger() == 0) {
i += 1
val num_cid = getcids(tm)
bc.putExtra(CURRENT_CID, num_cid .toString())
sendBroadcast(bc)
handler.postDelayed(this, 1000)
}
}
}, 0)
}
这里应该怎么做才能让每部手机和显示的通知都保持相同的速度?
【问题讨论】:
-
你能分享你的
IntentService代码吗?
标签: android kotlin background intentservice