android - 当我从后台小部件中终止应用程序时停止
问题描述
var service: PendingIntent? = null
val intentForService = Intent(context!!.applicationContext, NewAppWidget::class.java)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+5:30"))
val currentHour = cal.get(Calendar.HOUR_OF_DAY)
val currentMinutes = cal.get(Calendar.MINUTE)
val currentSeconds = cal.get(Calendar.SECOND)
val currentMicroSecods = cal.get(Calendar.MILLISECOND)
if (service == null) {
service = PendingIntent.getService(context, 0, intentForService, PendingIntent.FLAG_CANCEL_CURRENT)
val formater = DecimalFormat("00")
systemTimePast = "${formater.format(currentHour)}:${formater.format(currentMinutes)}:${formater.format(currentSeconds)}:${formater.format(currentMicroSecods.div(10))}"
}
alarmManager.setRepeating(AlarmManager.RTC, cal.time.time, 1000, service)
解决方案
从后台杀死后,只有一种方法可以运行您的应用程序!
该方法是通知访问。
在应用程序关闭后,您可以通过以下几种方式运行小部件:
在顶部通知栏的顶部显示您的应用小部件。
在后台显示您的通知类。
如果操作系统关闭了您的应用程序,那么您的通知类会自行重启。互联网上有很多网站,您编码的方法与它相关!
我希望先生,您的概念会很清楚。
推荐阅读
- json - 使用 Rest Assured 从返回的响应中提取多个 JsonPath 值
- dataweave - 如何将 mule dataweave 中的字段附加到现有的 json 有效负载
- python - python run bat file to setup environment variables
- api - Discord.js 不能多次更改频道名称
- google-app-engine - 谷歌应用引擎给我的默认域名如何更改?
- android - 从服务安排时,Android 的 WorkManager 未运行
- javascript - 如何在 Node.js 进程窗口之外获取键盘输入?
- tkinter - 使用 Python 的 tkinter 将输入字段传输到 excel 文件
- java - 如何在 TextReport 和 BrowserWebDriverContainer 上替换 JUnit 5 中的 @Rule 注释?
- c# - 通过 asp .net core 连接到 Google api 导致无限的授权循环