I am calling an AsyncTask to stop an previously started service.
But the ProgressDialog does not rotate while the Asyctask is running.
So I think that there is something wrong and I could get problems with an ANR error.
Any ideas?
new asyncTaskZieladresse().execute();
public class asyncTaskZieladresse extends AsyncTask<Void, Integer, Void> {
int progress;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
final Spinner Fahrerauswahl = (Spinner)findViewById(R.id.spinner1);
final Spinner Fahrzeugauswahl = (Spinner)findViewById(R.id.spinner2);
final Spinner Nutzungsartauswahl = (Spinner)findViewById(R.id.spinner3);
Cursor mcursor = (Cursor) Fahrerauswahl.getSelectedItem();
Fahrer = mcursor.getString(mcursor.getColumnIndexOrThrow("name"));
FahrerID = mcursor.getString(mcursor.getColumnIndexOrThrow("_id"));
mcursor.close();
Cursor mcursor1 = (Cursor) Fahrzeugauswahl.getSelectedItem();
Kennzeichen = mcursor1.getString(mcursor1.getColumnIndexOrThrow("fahrzeug_kennzeichen"));
KennzeichenID = mcursor1.getString(mcursor1.getColumnIndexOrThrow("_id"));
mcursor1.close();
Cursor mcursor2 = (Cursor) Nutzungsartauswahl.getSelectedItem();
Nutzungsart = mcursor2.getString(mcursor2.getColumnIndexOrThrow("nutzungsart"));
NutzungsartID = mcursor2.getString(mcursor2.getColumnIndexOrThrow("_id"));
mcursor2.close();
VariablenUebergebenGpsFahrt();
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog = ProgressDialog.show(Main.this, "GPS", "Ziel-Standort wird ermittelt...");
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
stopService(new Intent(Main.this, GPSService.class));
return null;
}
}
This will not help, as I have tried to explain in the comments to your question. If
onDestroy()is taking too long, you need to do that work sooner and in a background thread. Putting thestopService()call in a background thread is completely pointless.No, it is not.
onDestroy()is called on the main application thread. Always.Your service might also have a background thread of its own for other work, but the lifecycle methods (
onCreate(),onStartCommand(),onBind(), andonDestroy()) are always called on the main application thread.Because you are doing too much work in
onDestroy().onDestroy()is called on the main application thread (a.k.a., “UI-thread”).UPDATE based on first comment:
You never call
onDestroy(). Android callsonDestroy(). You are not Android.You seem to think that a service is a thread. It is not. You can tell this by reading the documentation (“Note that services, like other application objects, run in the main thread of their hosting process. “). Please read the documentation.