在vivo 1716设备中从后台终止时服务未运行

piv4azn7  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(230)
class LocationService : Service() {

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        intent?.let { it ->
            val action = it.action
            action?.let { it2 ->
                if (it2 == Constant.ACTION_START_LOCATION_SERVICE)
                    startLocationService()
                else if (it2 == Constant.ACTION_STOP_LOCATION_SERVICE)
                    stopLocationService()
            }
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent?): IBinder? {
        throw UnsupportedOperationException("Not Yet Implemented")
    }

    private val locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult) {
            super.onLocationResult(locationResult)
            val latitude = locationResult.lastLocation.latitude
            val longitude = locationResult.lastLocation.longitude
            Log.d("LocationUpdate", "Latitude: $latitude | Longitude: $longitude")
        }
    }

    @SuppressLint("MissingPermission")
    private fun startLocationService() {
        Log.d("LocationUpdate", "Service Started.")
        val channelId = "location_notification_channel"
        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val resultIntent = Intent()
        val pendingIntent = PendingIntent.getActivity(
            applicationContext,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )
        val builder = NotificationCompat.Builder(applicationContext, channelId).apply {
            setSmallIcon(R.mipmap.ic_launcher)
            setContentTitle("Location Service")
            setDefaults(NotificationCompat.DEFAULT_ALL)
            setContentText("Walking")
            setContentIntent(pendingIntent)
            setAutoCancel(true)
            priority = NotificationCompat.PRIORITY_HIGH
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager.getNotificationChannel(channelId) == null) {
                val notificationChannel = NotificationChannel(
                    channelId,
                    "Location Service Channel",
                    NotificationManager.IMPORTANCE_HIGH
                ).apply {
                    description = "This channel is used by location service."
                }
                notificationManager.createNotificationChannel(notificationChannel)
            }
        }

        val locationRequest = LocationRequest.create().apply {
            interval = 4000
            fastestInterval = 2000
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        }

        LocationServices.getFusedLocationProviderClient(this)
            .requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
        startForeground(Constant.LOCATION_SERVICE_ID, builder.build())
    }

    private fun stopLocationService() {
        Log.d("LocationUpdate", "Service Stopped.")
        LocationServices.getFusedLocationProviderClient(this)
            .removeLocationUpdates(locationCallback)
        stopForeground(true)
        stopSelf()
    }
}

我尝试了以下解决方案,但不起作用:
在oppo、vivo、mi android版本7.1.2中被终止后,后台服务未重新启动
在中国手机中刷卡时,应用程序后台服务停止
android中的后台服务没有运行oppo、vivo等设备
从最近版本中删除应用程序时后台服务停止
如何在打瞌睡模式下将应用程序列入白名单android 6.0

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题