首页 > 解决方案 > 当我从后台小部件中终止应用程序时停止

问题描述

 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)

标签: androidtimerwidget

解决方案


从后台杀死后,只有一种方法可以运行您的应用程序!

该方法是通知访问。

在应用程序关闭后,您可以通过以下几种方式运行小部件:

  1. 在顶部通知栏的顶部显示您的应用小部件。

  2. 在后台显示您的通知类。

如果操作系统关闭了您的应用程序,那么您的通知类会自行重启。互联网上有很多网站,您编码的方法与它相关!

我希望先生,您的概念会很清楚。


推荐阅读