gpt4 book ai didi

android - IntentService 已弃用,如何将其替换为 JobIntentService?

转载 作者:行者123 更新时间:2023-12-02 13:03:12 26 4
gpt4 key购买 nike

关注 FetchAddressIntentService使用 IntentService 实现(在 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"
}
}


有没有人建议我如何替换 IntentServiceJobIntentService以一种好的方式?
IntentService在 Android-R/Android-11 中已弃用。

我已经尝试关注这方面的一些帖子,但没有人指导正确的路径如何以 AddressFetchIntentService 使用 IntentService 的方式将 IntentService 调用的可用性更改为 JobIntentService 调用。 .

RG

最佳答案

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

您可以将服务启动为
    val intent = Intent(this, FetchAddressIntentService::class.java)
FetchAddressIntentService.enqueueWork(this, intent)

重要的:

您需要为 list 添加权限。
<uses-permission android:name="android.permission.WAKE_LOCK" />//pre-oreo 设备需要


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

阅读更多 here

添加于格林威治标准时间 2020.06.02 19:20 作者:Roar Grønmo

我将代码更改为:
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)
}
}

}

关于android - IntentService 已弃用,如何将其替换为 JobIntentService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62138507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com