Android后台服务未在OPPO,VIVO Mobile中运行

时间:2019-05-28 10:28:45

标签: android kotlin service

我正在应用程序中实现后台服务,它在模拟器和某些Real devices中可以正常工作。

当我尝试在OPPO/VIVO设备中运行应用程序时出现问题

它正在显示警报

  

应用无法启动多个服务,清除了历史记录并   建议重试

Error Image

我尝试清除历史记录,重新启动了Mobile,重新安装了应用程序,打开应用程序后错误仍然存​​在

我正在使用Google API客户端,因为位置服务不赞成使用融合位置

我当前遇到的错误是在API 27中

我的服务代码是

class LocationtrackService : Service(), GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

private val locationInterval:Long = 30000
private val fastLocationInterval:Long = 15000
private val logTag = "Bgservice"

private lateinit  var apiClient: GoogleApiClient
private var locationRequest = LocationRequest()
private var lastLocation: Location? = null
private lateinit  var locationClient: FusedLocationProviderClient

private lateinit var userId : String
private lateinit var baseUrl : String

private var callback: LocationCallback = object : LocationCallback() {

    override fun onLocationResult(locationResult: LocationResult) {
        val locationList = locationResult.locations
        if (locationList.size > 0) {

            val location = locationList[locationList.size - 1]

            Log.i(logTag, "Location: " + location.latitude + " " + location.longitude)

            lastLocation = location

            if (lastLocation != null) {
                sendMessageToUI(lastLocation!!.latitude.toString(), lastLocation!!.longitude.toString())
            }
        }
    }
}

override fun onCreate() {
    super.onCreate()

    Log.d(logTag, "onCreate")

    val channelId = "my_service"

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.app_name) + " name"
        val description = getString(R.string.app_name) + " description"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(channelId, name, importance)
        channel.description = description
        val notificationManager = getSystemService(NotificationManager::class.java)
        notificationManager!!.createNotificationChannel(channel)


        val notification = NotificationCompat.Builder(this, channelId)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("App is in background")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .build()

        startForeground(5465, notification)
    }
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {

    apiClient = GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build()

    locationRequest.interval = locationInterval
    locationRequest.fastestInterval = fastLocationInterval

    val priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    //PRIORITY_BALANCED_POWER_ACCURACY, PRIORITY_LOW_POWER, PRIORITY_NO_POWER

    locationRequest.priority = priority
    apiClient.connect()

    locationClient = LocationServices.getFusedLocationProviderClient(this)

    return START_STICKY
}

override fun onBind(intent: Intent): IBinder? = null

override fun onConnected(dataBundle: Bundle?) {

    Log.d(logTag, "Connected to Google API")
}

override fun onConnectionSuspended(i: Int) {
    Log.d(logTag, "Connection suspended")
}

private fun sendMessageToUI(lat: String, lng: String) {

    Log.d(logTag, "$lat - $lng")

}

override fun onConnectionFailed(connectionResult: ConnectionResult) {
    Log.d(logTag, "Failed to connect to Google API")
}



}

小小的帮助将不胜感激

0 个答案:

没有答案