Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentService is deprecated, how do I replace it with JobIntentService?

Following FetchAddressIntentService implementation with IntentService (in kotlin):

class FetchAddressIntentService  //Constructor of this service
    : IntentService(INTENTTAG) {
    //Receiver where results are forwarded from this service
    protected var resultReceiver: ResultReceiver? = null

    private val TAG by lazy { this::class.java.simpleName }

    //Intent Service handler
    override fun onHandleIntent(intent: Intent?) { //Errormessages
        var errorMessage = ""
        if (intent != null) {
            resultReceiver =
                intent.getParcelableExtra(RECEIVER)
        }
        //Checks if receiver was properly registered
        if (resultReceiver == null) {
            Log.wtf(
                TAG,
                "No reciever received. There is nowhere to send the results !!"
            )
            return
        }
        //Get the location passed to this service through an extra.
        var location: Location? = null
        if (intent != null) {
            location =
                intent.getParcelableExtra(LOCATION_DATA_EXTRA)
        }
        //Make sure the location is really sent
        if (location == null) {
            errorMessage = getString(R.string.gis_error_no_location_data_provided)
            Log.wtf(TAG, errorMessage)
            deliverResultToReceiver(FAILURE_RESULT, errorMessage)
            return
        }
        //Setting locale
        val geocoder = Geocoder(this, Locale.ROOT)
        //Address found
        var addresses: List<Address>? = null
        try {
            addresses = geocoder.getFromLocation(location.latitude, location.longitude, 1)
            Log.i(TAG,"rec: address = ${addresses[0]}")
        } catch (ioException: IOException) { //Catches network or i/o problems
            errorMessage = getString(R.string.gis_error_service_not_available)
            Log.e(TAG, errorMessage, ioException)
        } catch (illegalArgumentException: IllegalArgumentException) { //Error in latitude or longitude data
            errorMessage = getString(R.string.gis_error_invalid_lat_long_used)
            Log.e(
                TAG,
                errorMessage + ". Latitude = " + location.latitude +
                        ", Longitude = " + location.longitude,
                illegalArgumentException
            )
        }
        //Handles cases where no addresses where found
        if (addresses == null || addresses.isEmpty()) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.gis_error_no_address_found)
                Log.e(TAG, errorMessage)
            }
            deliverResultToReceiver(FAILURE_RESULT, errorMessage)
        } else {
            val address = addresses[0]
            //deliverAddressToReceiver(SUCCESS_RESULT, address)
            deliverAddressToReceiver2(SUCCESS_ADDRESS,address)
        }
    }

    private fun deliverAddressToReceiver2(
        resultCode: Int,
        address: Address
    ){
        val bundle = Bundle()
        bundle.putParcelable("address",address)
        resultReceiver?.send(resultCode,bundle)
    }

    private fun deliverResultToReceiver(resultCode: Int, message: String) {
        val bundle = Bundle()
        bundle.putString(RESULT_DATA_KEY, message)
        resultReceiver!!.send(resultCode, bundle)
    }

    companion object {
        private const val INTENTTAG = "FetchAddressIS"
    }
}

Do anyone have a suggestion how I can replace IntentService with JobIntentService in a good way ?

IntentService is deprecated in Android-R / Android-11.

I have tried following some posts on this, but no one is directing the correct path how to change the usability of the IntentService call to JobIntentService call in the way this AddressFetchIntentService is using IntentService.

RG

like image 732
Roar Grønmo Avatar asked Jun 01 '20 18:06

Roar Grønmo


People also ask

What can I use instead of IntentService on Android?

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.

Why is JobIntentService deprecated?

This class is deprecated. This class has been deprecated in favor of the Android Jetpack WorkManagerlibrary, which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts.

What is difference between IntentService and JobIntentService?

Both work same but the only difference with JobIntentService is that JobIntentService gets restarted if the application gets killed while the service was executing.

How do I use JobIntentService?

JobIntentService sample app Just Open the Android Studio, Go to File menu and create a new project, fill the project name (I'm using JobIntentService Example) and select the EmptyActivity template. Now create a new file with is MyJobIntentService which extends the JobIntentServcie.


1 Answers

class FetchAddressIntentService : JobIntentService() {

    // This method is called when service starts instead of onHandleIntent
    override fun onHandleWork(intent: Intent) {
        onHandleIntent(intent)
    }

    // remove override and make onHandleIntent private.
   private fun onHandleIntent(intent: Intent?) {}

    // convenient method for starting the service.
    companion object {
        fun enqueueWork(context: Context, intent: Intent) {
            enqueueWork(context, FetchAddressIntentService::class.java, 1, intent)
        }
    }
}

You can start the service as

    val intent = Intent(this, FetchAddressIntentService::class.java)
    FetchAddressIntentService.enqueueWork(this, intent)

Important:

You need to add permissions to manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" /> // needed for pre-oreo devices

also

<service
    android:name=".FetchAddressIntentService"
    android:permission="android.permission.BIND_JOB_SERVICE" // needed for oreo and above
    android:exported="true"/>

Read more here


ADDED 2020.06.02 19:20 GMT By Roar Grønmo

I changed my code to:

class FetchAddressJobIntentService:JobIntentService()
{
    private val TAG by lazy { this::class.java.simpleName }

    protected var resultReceiver: ResultReceiver? = null

    /*
    override fun onHandleWork(intent: Intent) {
        onHandleIntent(intent)
    }*/

    override fun onHandleWork(intent: Intent) { //Errormessages
        var errorMessage = ""

        resultReceiver = intent.getParcelableExtra(RECEIVER)

        //Checks if receiver was properly registered
        if (resultReceiver == null) {
            Log.wtf(
                TAG,
                "No reciever received. There is nowhere to send the results !!"
            )
            return
        }
        //Get the location passed to this service through an extra.
        var location: Location? = null

        location = intent.getParcelableExtra(LOCATION_DATA_EXTRA)

        //Make sure the location is really sent
        if (location == null) {
            errorMessage = getString(R.string.gis_error_no_location_data_provided)
            Log.wtf(TAG, errorMessage)
            deliverResultToReceiver(FAILURE_RESULT, errorMessage)
            return
        }
        //Setting locale
        val geocoder = Geocoder(this, Locale.ROOT)
        //Address found
        var addresses: List<Address>? = null
        try {
            addresses = geocoder.getFromLocation(location.latitude, location.longitude, 1)
            Log.i(TAG,"rec: address = ${addresses[0]}")
        } catch (ioException: IOException) { //Catches network or i/o problems
            errorMessage = getString(R.string.gis_error_service_not_available)
            Log.e(TAG, errorMessage, ioException)
        } catch (illegalArgumentException: IllegalArgumentException) { //Error in latitude or longitude data
            errorMessage = getString(R.string.gis_error_invalid_lat_long_used)
            Log.e(
                TAG,
                errorMessage + ". Latitude = " + location.latitude +
                        ", Longitude = " + location.longitude,
                illegalArgumentException
            )
        }
        //Handles cases where no addresses where found
        if (addresses == null || addresses.isEmpty()) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.gis_error_no_address_found)
                Log.e(TAG, errorMessage)
            }
            deliverResultToReceiver(FAILURE_RESULT, errorMessage)
        } else {
            val address = addresses[0]
            //deliverAddressToReceiver(SUCCESS_RESULT, address)
            deliverAddressToReceiver2(SUCCESS_ADDRESS,address)
        }
    }

    private fun deliverAddressToReceiver2(
        resultCode: Int,
        address: Address
    ){
        val bundle = Bundle()
        bundle.putParcelable("address",address)
        resultReceiver?.send(resultCode,bundle)
    }

    private fun deliverResultToReceiver(resultCode: Int, message: String) {
        val bundle = Bundle()
        bundle.putString(RESULT_DATA_KEY, message)
        resultReceiver!!.send(resultCode, bundle)
    }



    companion object{
        fun enqueueWork(context: Context, intent: Intent){
            enqueueWork(context,FetchAddressJobIntentService::class.java, 1, intent)
        }
    }

}

like image 143
Max Avatar answered Sep 20 '22 15:09

Max