I'm implementing background service in my application and it works fine in emulator and some Real devices.

the problem occurs when I'm trying to run app in OPPO/VIVO Devices

it is showing an alert

App has failed to start up multiple services, clearing the history and trying again is recommended

Error Image

I tried clearing history, restarted Mobile, reinstalled app and error remains same after i open the application

I'm using Google API client as fused location is deprecated for Location services

the Current error I'm getting is in API 27

My Service Code is

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")
}



}

little help would be greatly appreciated

0 Answers