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

628mspwn  于 2023-04-10  发布在  Android
关注(0)|答案(2)|浏览(184)

这是一个棘手的问题.我有一个应用程序,在后台收集GPS数据(与前台服务).此外,我设置3闹钟,应该在一个小时内运行的帧.所有这一切都是在设备中安装MDM(SOTI,如果它的任何帮助).
好吧,当应用程序在前台时,没有问题,GPS数据被正确收集,警报在必要时会触发。问题是当我锁定设备或屏幕关闭时。通常情况下,它会收集更多的GPS数据,几分钟后,应用程序和前台服务将被杀死,无论如何。
该设备绝对不需要资源,因为它是MDM允许的唯一应用程序,并且没有错误,因为我已经实现了crashlytics,它没有提供任何东西。
作为参考,由于我不能发布太多代码,我按预期启动前台服务(使用startForegroundService),并在服务的onCreate()方法中调用startForeground。此外,我在服务中有一个wakelock,但这根本没有帮助。
闹钟是用alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,startDate,period,pendingIntent)设置的,但是当应用处于后台时,它们不会触发。当我再次启动应用时,它们会重置并触发。
为什么我的应用程序被杀的线索?我使用的设备是华为Y6,如果它的任何帮助.我已经检查了https://dontkillmyapp.com/,并作出了所有的事情,我可以尝试不让我的应用程序被杀,但我失败了.此外,我已经检查了前台服务转储手机的信息和服务被标记为前台服务,因为它应该,优先级为4(我检查的时间),所以它不应该被杀死...

njthzxwz

njthzxwz1#

当状态转到onStop时,您需要一个广播接收器来重新启动服务。
Never ending service

nqwrtyyt

nqwrtyyt2#

这是一个服务,我使用一些GPS跟踪.重要的是一个服务,以保持运行是一个通知绑定到它.从我所知道的通知是让用户知道在后台运行的服务.
在start命令中,创建具有最高优先级的粘性通知。
希望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)
    }
}

相关问题