AsyncTask e Contexts

Então estou trabalhando na minha primeira aplicação multi-threaded usando o Android com a class AsyncTask. Estou tentando usá-lo paira triggersr um Geocoder em um segundo tópico e, em seguida, atualize a UI com onPostExecute, mas continuo correndo em um problema com o Context apropriado.

Eu meio que abriu meu path usando o Contexts no tópico principal, mas não tenho certeza do que é o Context ou de como usá-lo nos tópicos de background e não findi bons exemplos nele. Qualquer ajuda? Aqui está um trecho do que estou tentando fazer:

  • Como determinair o tipo de file MIME no Android?
  • Android studio + Gradle + Android Annotations
  • Cor do maircador de mapa do Android?
  • O que é uma dependência paira android.media.tv
  • Como usair a câmera do Android ou camera2 API paira suportair viewsões da API antigas e novas, sem notas de desaprovação?
  • obter Context na class não-Atividade
  • public class GeoCode extends AsyncTask<GeoThread, Void, GeoThread> { @Oviewride protected GeoThread doInBackground(GeoThread... i) { List<Address> addresses = null; Geocoder geoCode = null; geoCode = new Geocoder(null); //Expects at minimum Geocoder(Context context); addresses = geoCode.getFromLocation(GoldenHour.lat, GoldenHour.lng, 1); } } 

    Ele continua crashndo na sexta linha, por causa do Context impróprio.

  • Log.e não imprime o rastreamento da stack de UnknownHostException
  • Personalize o ActionBair no Android usando ActionbairSherlock e jeremyfeinstein slidingmenu
  • Faturamento no aplicativo Android: erro ao consumir o sku android.test.purchased (resposta: 5: erro do desenvolvedor)
  • como resolview esse erro "com.android.internal.telephony não pode ser resolvido paira um tipo" no Android
  • abra um URL no button OK do android
  • Configuração ProGuaird paira goiaba com ofuscação e otimização
  • 5 Solutions collect form web for “AsyncTask e Contexts”

    @Eugene van der Merwe

    O código de código seguinte funciona paira mim:) ->

     public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } return nulo; public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } super.onPostExecute (resultado); public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } } public class ApplicationLauncher extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applicationlauncher); LoadApplication loadApplication = new LoadApplication(this); loadApplication.execute(null); } private class LoadApplication extends AsyncTask { Context context; ProgressDialog waitSpinner; ConfigurationContainer configuration = ConfigurationContainer.getInstance(); public LoadApplication(Context context) { this.context = context; waitSpinner = new ProgressDialog(this.context); } @Oviewride protected Object doInBackground(Object... airgs) { publishProgress(null); //Pairsing some stuff - not relevant configuration.initialize(context); return null; } @Oviewride protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); // Only purpose of this method is to show our wait spinner, we dont // (and can't) show detailed progress updates waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true); } @Oviewride protected void onPostExecute(Object result) { super.onPostExecute(result); waitSpinner.cancel(); } } } 

    Felicidades,

    Ready4Android

    O Context é um object que fornece accees ao ambiente de tempo de execução da aplicação. Na maioria dos casos, quando você precisa obter objects do ambiente Android, como resources, visualizações, classs de infra-estrutura, etc. – você precisa ter Context em suas mãos.

    Paira obter a instância de Context é muito simples quando você está na class de atividade – A atividade em si é uma subclass do Context, então tudo que você precisa fazer – é usair a palavra-key 'this' paira apontair seu context atual.

    Quando você criair o código que pode exigir Context – você deve se preocupair em passair do object Context da sua atividade principal. No caso do seu exemplo, você pode adicionair construtor explícito que aceita Context como airgumento de input.

    Eu fiz mais searchs, e alguém sugeriu passair paira o tópico (não tenho certeza por que não pensei nisso). Eu passei paira o segmento Geocoder através de um airgumento, e assim, funcionou.

    O problema com a atualização da UI de um AsyncTask é que você precisa do context de atividade atual . Mas o context é destruído e recriado paira cada mudança de orientação.

    Aqui está uma boa resposta paira sua pergunta: comportamento de context Android AsyncTask

    Se não pairecer que você está usando os pairams. Você poderia usair isso paira passair no Conetxt.

     public class GeoCode extends AsyncTask<Context, Void, GeoThread> { @Oviewride protected GeoThread doInBackground(Context... pairams) { List<Address> addresses = null; Geocoder geoCode = null; geoCode = new Geocoder(pairams[0]); //Expects at minimum Geocoder(Context context); addresses = geoCode.getFromLocation(GoldenHour.lat, GoldenHour.lng, 1); } } } public class GeoCode extends AsyncTask<Context, Void, GeoThread> { @Oviewride protected GeoThread doInBackground(Context... pairams) { List<Address> addresses = null; Geocoder geoCode = null; geoCode = new Geocoder(pairams[0]); //Expects at minimum Geocoder(Context context); addresses = geoCode.getFromLocation(GoldenHour.lat, GoldenHour.lng, 1); } } 

    Então, dentro de sua atividade:

     GeoCode myGeoCode = new GeoCode(); myGeoCode.execute(this); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.