Esta es mi primera publicación de stackflow, así que sea amable conmigo. Estoy seguro de que lo que estoy tratando de hacer es posible y es algo que he hecho (o no he hecho?) Que está causando el problema ... No estoy seguro de qué es ese algo es.Android ProgressDialog no gira
lo que estoy tratando de hacer:
muestran una ProgressDialog mientras mis Sincroniza aplicaciones y procesa los datos descargados.
El problema:
Los espectáculos ProgressDialog pero no gira (que hace que parezca que se ha congelado), la sincronización y el procesamiento ocurre, el ProgressDialog cierra, la aplicación continúa de forma normal.
Cómo Actualmente estoy Tring para hacer eso:
Crear una ProgressDialog - en mi actividad
hacer la sincronización - en mi servicio
procesar los datos - en mi servicio
Dismis la ProgressDialog - en mi actividad
Las cosas que he intentado:
el uso de un hilo (código de abajo) el uso de un AsynTask (puede proporcionar el código si es necesario) Usando un controlador (puede proporcionar el código si es necesario)
Después de pasar mucho tiempo buscando la respuesta a mi pregunta, parece que otros han tenido el mismo problema o similar y han logrado resolverlo usando una de las ideas anteriores. Sin embargo, no he podido implementar ninguna de estas ideas para resolver mi problema en particular. Esto me asegura que es algo que estoy haciendo mal ... No estoy seguro de qué.
El código original que escribió y estoy usando como base para todos mis intentos de correcciones:
Primera
public class myActivity extends ListActivity {
private ProgressDialog dialog;
private boolean mIsBound;
private myService mBoundService;
private ServiceConnection mConnection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if we need to login start login activity for result*/
...
}
...
/*other methods*/
...
}
continuación
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode){
case ACTIVITY_LOGIN:
/*after login we know we always need to sync*/
dialog = new ProgressDialog(myActivity.this);
dialog.setMessage("Synchronising...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Thread doBind = new Thread(new Runnable(){public void run(){doBindService();}});
doBind.start();
break;
}
}
por lo que ahora estoy asumiendo que doBind está sucediendo en un hilo diferente, dejando el hilo de la interfaz de usuario sin nada que hacer aparte de mostrar el diálogo progresivo ...?
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
/*wait for service to finish before doing anything else... myMethod() is expecting the sync to have completed*/
/*I suspect this loop to be the thing causing the problem. But I'm not sure why because it is in a different thread so shouldn't interfear with the dialog?! And if that is what is causing the problem then how else can I do this?*/
}
/*get the activity to do Stuff with the downloaded data*/
myMethod();
/*finished with the service for now so unbind*/
doUnbindService();
if (dialog != null) {
dialog.dismiss();
}
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
private void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
Háganme saber si necesita más información para ayudarme con este problema.
EDIT:
Aquí está el código que estoy utilizando para utilizar un controlador en su lugar. Esto muestra el mismo comportamiento (el girador no gira) como antes.
Thread doBind = new Thread(new Runnable(){
public void run(){
doBindService();
}
});
doBind.start();
.
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
.
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
}
//...do stuff same as before...
handler.sendEmptyMessage(0);
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
Veo este mismo comportamiento de congelación en mi diálogo de progreso. Por lo general, es cuando la tarea está casi terminada y el diálogo está por cerrarse. ¿Por cuánto tiempo está tu diálogo? Si haces que el hilo duerma durante unos segundos, ¿eso hace que el diálogo gire? – softarn
Tengo un problema similar con ProgressDialog al que pregunté aquí: http://stackoverflow.com/questions/3821306/progressdialog-created-from-oncreatedialog-stops-animating-on-second-run. Creo que hay un error en la base de código de Android. – ageektrapped
@softarn: Gracias por la sugerencia. Si hago: Tema doBind = new Thread (nueva Ejecutable() { \t public void run() {try { \t \t \t \t Thread.sleep (10000); \t \t} catch (InterruptedException e) { \t \t \t e.printStackTrace(); \t \t} doBindService(); \t} }); El cuadro de diálogo gira durante 10 segundos y luego se congela durante 3 a 4 segundos antes de desaparecer ... – John