具有前台服务的屏幕关闭时,应用程序正在关闭

时间:2019-09-11 06:15:41

标签: android alarmmanager foreground-service

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

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

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

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

使用onCreate()设置了闹钟,但是当应用程序在后台运行时不会触发。当我再次启动该应用程序时,它们将被重置并触发。

关于为什么我的应用被杀死的任何线索?如果有帮助,我使用的设备是alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, period, pendingIntent。我已经检查过this link,并做了所有尝试以免杀死我的应用程序,但失败了。另外,我检查了转储电话信息的前台服务,并将该服务标记为应有的前台服务,其优先级为4(我检查的时间),因此不应被杀死...

谢谢你!

2 个答案:

答案 0 :(得分:0)

当状态变为onStop时,您需要一个广播接收器以再次启动该服务。 这是关于enter link description here

的不错的教程

Never ending service

答案 1 :(得分:0)

这是我用于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)
    }
}
相关问题