问题描述
我正在进行位置选择器活动,以检索“标记”位置(使用onCameraIdleListener)并使用Geocoder getFromLocation()生成地址信息。在真实设备上运行正常。
长话短说,我阅读了有关此问题的所有其他主题,并且确实实现了自定义JobIntentService来解决此问题。但是奇怪的是,在我的仿真器(Pixel 3a XL API 26)上,它似乎在首次尝试调用getFromLocation()时成功检索了地址信息,但随后的所有尝试均引发IOException grpc失败。
我尝试根据以下答案使用JobIntentService:https://stackoverflow.com/a/62140756/12338956
class FetchAddressJobIntentService:JobIntentService()
{
private val tag = "ADDRESS_SERVICE"
private var resultReceiver: ResultReceiver? = null
/*
override fun onHandleWork(intent: Intent) {
onHandleIntent(intent)
}*/
override fun onHandleWork(intent: Intent) {
var errorMessage = ""
resultReceiver = intent.getParcelableExtra(RECEIVER)
//Checks if receiver was properly registered
//MainActivity must pass the receiver as bundle extra
if (resultReceiver == null) {
Log.wtf(tag,"No receiver received. There is nowhere to send the results")
return
}
//Get the location passed to this service through an extra.
val location: Location? = intent.getParcelableExtra(LOCATION_DATA_EXTRA)
//Make sure the location is really sent
if (location == null) {
errorMessage = "no location data provided"
Log.wtf(tag,errorMessage)
deliverResultToReceiver()
return
}
//Setting locale
val geocoder = Geocoder(this,Locale.ROOT)
//Address found
Log.e(tag,location.toString())
var addresses: List<Address>? = null
try {
addresses = geocoder.getFromLocation(location.latitude,location.longitude,1)
} catch (ioException: IOException) { //Catches network or i/o problems
errorMessage = "service not available"
Log.e(tag,errorMessage,ioException)
} catch (illegalArgumentException: IllegalArgumentException) { //Error in latitude or longitude data
errorMessage = "invalid latitude or longitude"
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 = "no address found"
Log.e(tag,errorMessage)
}
deliverResultToReceiver()
} else {
val address = addresses[0]
deliverAddressToReceiver(address)
}
}
private fun deliverAddressToReceiver(
address: Address
){
val bundle = Bundle()
bundle.putParcelable(ADDRESS,address)
resultReceiver?.send(SUCCESS_ADDRESS,bundle)
}
private fun deliverResultToReceiver() {
val bundle = Bundle()
bundle.putString(RESULT_DATA_KEY,"1")
resultReceiver!!.send(FAILURE_RESULT,bundle)
}
companion object
{
@JvmStatic
fun enqueueWork(mapsActivity: MapsActivity,intent: Intent)
{
enqueueWork(mapsActivity,FetchAddressJobIntentService::class.java,1,intent)
}
}
}
在我的MapsActivity onCreate()中:
addressResultReceiver = new AddressResultReceiver(new Handler(Looper.getMainLooper()));
if (fusedLocationProviderClient != null)
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this,location -> {
// Got last known location. In some rare situations this can be null.
if (location != null) {
currentLocation = location;
currentLatLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,15));
}
});
AddressResultReceiver类:
private class AddressResultReceiver extends ResultReceiver
{
AddressResultReceiver(Handler handler)
{
super(handler);
}
//Receives data sent from FetchAddressJobIntentService and updates the UI in MainActivity.
@Override
protected void onReceiveResult(int resultCode,Bundle resultData)
{
if (resultCode == SUCCESS_ADDRESS)
{
Address a = resultData.getParcelable(ADDRESS);
if (a != null)
markerUserLocation = new UserLocation("Marker Location",a);
}
}
}
在相机空闲时开始意图:
GoogleMap.OnCameraIdleListener onCameraIdleListener = () -> {
LatLng latLng = map.getCameraPosition().target;
markerLocation = new Location("Camera Location");
markerLocation.setLatitude(latLng.latitude);
markerLocation.setLongitude(latLng.longitude);
Intent intent = new Intent(this,FetchAddressJobIntentService.class);
intent.putExtra(RECEIVER,addressResultReceiver);
//pass the marker location
intent.putExtra(LOCATION_DATA_EXTRA,markerLocation);
FetchAddressJobIntentService.enqueueWork(MapsActivity.this,intent);
};
map.setOnCameraIdleListener(onCameraIdleListener);
map.setOnCameraMoveStartedListener(onCameraMoveStartedListener);
它在应用程序首次在模拟器上运行时成功检索了地址信息,但抛出IOException:当我更改相机(标记)位置时,grpc失败。我尝试重新启动应用程序和模拟器但它仍然仅在应用启动时起作用。
Logcat在应用启动并显示地址时未显示错误
2020-10-13 03:38:52.164 17167-17263/com.digitalsmart.mutify E/ADDRESS_SERVICE: Location[Camera Location 37.421998,-122.084000 hAcc=??? t=?!? et=?!? vAcc=??? sAcc=??? bAcc=???]
2020-10-13 03:38:52.590 17167-17181/com.digitalsmart.mutify I/zygote: Do full code cache collection,code=248KB,data=194KB
2020-10-13 03:38:52.592 17167-17181/com.digitalsmart.mutify I/zygote: After code cache collection,code=236KB,data=155KB
2020-10-13 03:38:52.660 17167-17181/com.digitalsmart.mutify I/zygote: Do partial code cache collection,code=245KB,data=167KB
2020-10-13 03:38:52.661 17167-17181/com.digitalsmart.mutify I/zygote: After code cache collection,data=167KB
2020-10-13 03:38:52.661 17167-17181/com.digitalsmart.mutify I/zygote: Increasing code cache capacity to 1024KB
2020-10-13 03:38:59.398 17167-17247/com.digitalsmart.mutify D/EGL_emulation: eglMakeCurrent: 0xa44865c0: ver 3 1 (tinfo 0xa4483700)
2020-10-13 03:38:59.402 17167-17167/com.digitalsmart.mutify D/CanvasSaveProxy: New LegacyCanvasSaveProxy
2020-10-13 03:38:59.450 17167-17247/com.digitalsmart.mutify D/EGL_emulation: eglMakeCurrent: 0xa44865c0: ver 3 1 (tinfo 0xa4483700)
在我拖动摄像机后,Logcat显示从onHandleWork()抛出的grpc异常
2020-10-13 03:41:09.827 17167-17437/com.digitalsmart.mutify E/ADDRESS_SERVICE: Location[Camera Location 37.410983,-122.091018 hAcc=??? t=?!? et=?!? vAcc=??? sAcc=??? bAcc=???]
2020-10-13 03:41:14.830 17167-17437/com.digitalsmart.mutify E/ADDRESS_SERVICE: service not available
java.io.IOException: grpc failed
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.digitalsmart.mutify.util.FetchAddressJobIntentService.onHandleWork(FetchAddressJobIntentService.kt:71)
at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:392)
at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:383)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
我100%确信应用启动时从意图服务返回了地址信息,并且直到onReceiveResult()才初始化“ markerLocation”。
有什么想法吗?
更新: 我以某种方式通过在模拟器上打开飞行模式来修复它... 打开飞行模式并打开wifi,现在当我拖动相机时,它正在更新地址。
在这一点上,我很确定这是一个模拟器问题。但是,如果有可能是代码问题,欢迎任何帮助/想法/解释。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)