The error seems in the log every times that I search a new address in a AutocompleteActivity from the second time onwards.
I am making an Autocomplete functionality in an Android application to API 33 with the Google API Place Autocomplete for Android, the goal of this application is the user to be able to write an address and does a selection on it, later the app to get information about the local selected and to show it on the map.
For that, first the app initializes the API, specifies which types of place data to return after the user has made a selection, calls the AutocompleteActivityMode.FULLSCREEN thought of an intent, and finally when the user has made a selection, the app gets a latitude and a longitude. Everything works in relation of autocomplete, the address shows up and the app gets the location, however the error Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true seems in the log every times that I search a new address in a AutocompleteActivity from the second time onwards. In some research, I found the reason could be something about the communication channel between the API and the application isn’t being finished correctly, it’s being necessary to do shutdown, but I didn’t find anything about it in the official documentation. How could I fix this error?
The error:
E/zzbfw: *~*~*~ Previous channel {0} was not shutdown properly!!! ~*~*~*
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
at com.google.android.libraries.places.internal.zzbfv.<init>(com.google.android.libraries.places:places@@3.3.0:3)
at com.google.android.libraries.places.internal.zzbfw.<init>(com.google.android.libraries.places:places@@3.3.0:2)
at com.google.android.libraries.places.internal.zzbfu.zza(com.google.android.libraries.places:places@@3.3.0:18)
at com.google.android.libraries.places.internal.zzatj.zza(com.google.android.libraries.places:places@@3.3.0:1)
at com.google.android.libraries.places.internal.zzhr.zza(com.google.android.libraries.places:places@@3.3.0:24)
at com.google.android.libraries.places.api.Places.zza(com.google.android.libraries.places:places@@3.3.0:7)
at com.google.android.libraries.places.widget.internal.ui.zzh.<init>(com.google.android.libraries.places:places@@3.3.0:7)
at com.google.android.libraries.places.widget.AutocompleteActivity.onCreate(com.google.android.libraries.places:places@@3.3.0:12)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is the code that I am using:
if (id == R.id.action_search_address) {
if(!Places.isInitialized()){
// Define a variable to hold the Places API key.
String apiKey = "Mykey";
// Initialize the SDK
Places.initialize(getApplicationContext(), apiKey);
}
List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
.build(this);
startAutocomplete.launch(intent);
}
return super.onOptionsItemSelected(item);
}
private final ActivityResultLauncher<Intent> startAutocomplete = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
if (intent != null) {
Place place = Autocomplete.getPlaceFromIntent(intent);
Log.i(TAG_LOG, "Address: " + place.getAddress() + ", Longitude: " + place.getLatLng().longitude + " e Latitude: " + place.getLatLng().latitude+ "");
mapWebView.evaluateJavascript("viewAddress(" + place.getLatLng().latitude + "," + place.getLatLng().longitude + ", \"" + place.getAddress() + "\")", null);
}
} else if (result.getResultCode() == Activity.RESULT_CANCELED) {
// The user canceled the operation.
Log.i(TAG_LOG, "User canceled autocomplete");
}
});
The settings to use the API are :
settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
build.gradle (module)
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
}
}
allprojects {
repositories {
jcenter()
google()
mavenCentral()
}
}
build.gradle (module) dependencies:
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.21"))
implementation 'com.google.android.libraries.places:places:3.3.0'