Access to GoogleMap from outside the user interface in Android

I am using Google Maps Android API v2 for my Android application. There I initialize the GoogleMap as follows: onCreate

SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); mGoogleMap = fm.getMap(); 

In my application, I need to perform some background tasks, and I use AsybcTask for this. Although progress is in the background, I want to update the map. Please refer to the following sample.

 @Override protected Void doInBackground(Void...voids) { while(Condition) { mGoogleMap.clear(); mGoogleMap.addMarker(marker); Thread.sleep(5000); } } 

I know that I cannot directly access user interface components inside the doInBackground method. Access to common user interface components, such as buttons, edittexts, textviews, can be obtained by analyzing the application context in the AsyncTask class. But I can’t find a way to access GoogleMap from the doInBackground method. Could you give me a solution.

+5
source share
1 answer

if your AsyncTask is part of an Activity , you can simply use the runOnUiThread method, for example:

 @Override protected Void doInBackground(Void...voids) { while(Condition) { runOnUiThread(new Runnable() { @Override public void run() { mGoogleMap.clear(); mGoogleMap.addMarker(marker); Thread.sleep(5000); } }); } } 
+5
source

Source: https://habr.com/ru/post/1206987/


All Articles