Android - Como pausar y reanudar un hilo

 
Vista:

Como pausar y reanudar un hilo

Publicado por Russel Osorio (3 intervenciones) el 23/06/2016 22:40:50
hola, la problematica es la siguiente.


tengo 2 lineanlayout (uno rojo y otro azul) cada uno ocupa media pantalla , el rojo esta arriba y el azul abajo(el azul esta de un tamaño pequeño),
lo que necesito es usar los hilos para agrandar el layout azul cada segundo un poco mas para causar el efecto de que el layout se despliega al presionar un botón ,

en resumen quiero un layout que se despliegue y se oculte o se des oculte y pienso utilizar hilos pero si existe una forma mas fácil se los agradecería mucho esto es lo que tengo hasta ahora


1
2
3
4
5
6
7
8
private void tareaLarga()
{
    try {
       LinearLayout layout = (LinearLayout)findViewById(R.id.layoutazul);
            layout.setLayoutParams(new LinearLayout.LayoutParams(700,200));
            Thread.sleep(1000);
    } catch(InterruptedException e) {}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Como pausar y reanudar un hilo

Publicado por Daniel Alejandro Rosas Vazquez (2 intervenciones) el 04/07/2016 20:27:41
Hola que tal hace poco tuve un problema parecido y este código me sirvió espero y a tu también


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package example;
 
import java.util.Date;
 
/**
* Example of player with play/pause/stop controls
*
* @author drosas2016.tic@gmail.com
*/
public class PauseExample implements Runnable {
 
private boolean paused = false;
private boolean stopped = false;
 
public void play() {
paused = false;
stopped = false;
new Thread(this, "Player").start();
}
 
public synchronized void pause() {
paused = true;
}
 
public synchronized void resume() {
paused = false;
notify();
}
 
public synchronized void stop() {
stopped = true;
// If it was paused then resume and then stop
notify();
}
 
PauseExample() {
}
 
public void run() {
Date t0 = new Date();
while (!stopped) {
try {
synchronized (this) {
if (paused) {
System.out.println("Paused");
wait();
System.out.println("Resumed");
}
}
long secs = ((new Date()).getTime() - t0.getTime()) / 1000;
System.out.println(secs);
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.err.println(ex);
}
}
}
 
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws InterruptedException {
PauseExample p = new PauseExample();
p.play();
Thread.sleep(5000);
p.pause();
Thread.sleep(5000);
p.resume();
Thread.sleep(5000);
p.stop();
}
 
}


Salida del programa (los segundos en los que hace pausa pueden variar ligeramente):

0
1
2
3
4
Paused
Resumed
10
11
12
13
14
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar