【问题标题】:App is being closed while screen off while having foreground service在有前台服务时屏幕关闭时应用程序正在关闭
【发布时间】:2019-09-11 16:09:56
【问题描述】:

这是一个棘手的问题。我有一个在后台收集GPS 数据的应用程序(使用前台服务)。此外,我设置了 3 个应该在几个小时内运行的警报。所有这些都是在设备中安装了MDM 时完成的(SOTI,如果有帮助的话)。

好吧,当应用程序在前台时没有问题,GPS 数据被正确收集,警报在必要时触发。 问题是当我锁定设备或屏幕关闭时。通常,它会收集更多的GPS 数据,并在几分钟内无论如何都会终止应用程序和前台服务。

该设备绝对不需要资源,因为它是MDM 允许的唯一应用程序,并且没有错误,因为我已经实现了 crashlytics 并且它没有提供任何东西。

作为参考,由于我不能发布太多代码,我按预期启动前台服务(使用startForegroundService),并在服务的onCreate() 方法中调用startForeground。另外,我在服务中有一个唤醒锁,但这根本没有帮助。

警报设置为alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, period, pendingIntent),但当应用程序在后台时,它们不会触发。当我再次启动应用程序时,它们会被重置并触发。

任何关于为什么我的应用程序被杀死的线索?我使用的设备是Huawei Y6,如果有帮助的话。我已经检查了this link 并尽我所能尝试不让我的应用程序被杀死,但我失败了。另外,我检查了转储手机信息的前台服务,该服务被标记为前台服务,优先级为 4(我检查的时间),所以它不应该被杀死......

谢谢你!

【问题讨论】:

    标签: android alarmmanager foreground-service


    【解决方案1】:

    当状态变为 onStop 以重新启动服务时,您需要一个广播接收器。 这是一个很好的教程enter link description here

    Never ending service

    【讨论】:

    • 嗨!感谢您的回复,我已经尝试过了,但没有运气:( 应用程序无论如何都会被杀死......
    【解决方案2】:

    这是我用于一些 GPS 跟踪的服务。服务保持运行的重要一点是绑定到它的通知。据我所知,通知是为了让用户知道在后台运行的服务。

    在启动命令中创建一个具有最高优先级的粘性通知。

    希望 Kotlin 代码没问题:

    class TrackService : Service(), LocationListener {
    
        private val tag = TrackService::class.java.simpleName
    
        companion object {
            private const val ONGOING_NOTIFICATION_ID = 3947
            const val FLAG_TICKET_RUNNING = 1
        }
    
        private var locationManager: LocationManager? = null
        private lateinit var notificationBuilder: NotificationCompat.Builder
    
    
        /**
         * Start command of service. Contains all starting commands needed to start the tracking
         *
         * @param intent used to call startService
         * @param flags flags used for service
         * @param startId id of service
         * @return type of service, in our case STICKY
         */
        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
            event = intent!!.getParcelableExtra("event")
            course = intent.getParcelableExtra("course")
    
            notificationBuilder = createNotificationBuilder()
            showNotification()
    
            return START_REDELIVER_INTENT
        }
    
    
        /**
         * Called when service is being stopped. This is where we stop all listeners and set the status
         * to "offline"
         */
        override fun onDestroy() {
            dismissNotification()
    
            super.onDestroy()
        }
    
    
        /**
         * Not needed for our use case
         * @param intent Intent
         * @return null
         */
        override fun onBind(intent: Intent): IBinder? = null
    
    
        /**
         * Shows the notification that makes the service more STICKY
         */
        private fun showNotification() {
            if (App.showNotificationContent())
                notificationBuilder.setContentText("Time: - | Distance: -")
            startForeground(ONGOING_NOTIFICATION_ID, notificationBuilder.build())
        }
    
    
        /**
         * Generates the notification to be shown
         *
         * @return NotificationCompat.Builder
         */
    
        private fun createNotificationBuilder(): NotificationCompat.Builder {
            if (pendingIntent == null) {
                val notificationIntent = Intent(this, DashboardActivity::class.java)
                notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
                notificationIntent.putExtra("ticket_flag", FLAG_TICKET_RUNNING)
                pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    
                @SuppressLint("NewApi")
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val notificationChannel = NotificationChannel("channel-4", "Tracking Channel 4", NotificationManager.IMPORTANCE_LOW)
                    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                    notificationManager.createNotificationChannel(notificationChannel)
                    notificationChannel.setSound(null, null)
                }
            }
    
            return NotificationCompat.Builder(this, "channel-4")
                    .setSmallIcon(R.drawable.ic_notification_orienteering)
                    .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
                    .setContentTitle(event.name + " - " + course.name)
                    .setAutoCancel(false)
                    .setSound(null)
                    .setOngoing(true)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
        }
    
    
        /**
         * This is the method that can be called to update the Notification
         */
        private fun updateNotification(time: String, distance: String) {
            if (App.showNotificationContent()) {
                notificationBuilder.setContentText("Time: $time | Distance: $distance")
                val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notificationBuilder.build())
            }
        }
    
    
        /**
         * Dismisses the notification
         */
        private fun dismissNotification() {
            stopForeground(true)
        }
    }
    

    【讨论】:

    • 嗨!感谢您的代码,我或多或少像您一样,但是我更改了通知通道的优先级并添加了setongoing,我看看它是否可以这样...再次感谢您!
    • @misterpresid 任何阳性结果?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多