Je suis en train de développer une application Android et j'ai un problème pour lancer un nouveau thread en arrière-plan.Discussion Android en arrière-plan
Je fais une classe avec ce code:
public class Downloader implements Runnable {
private Vector <DownloadRequest>messages = new Vector<DownloadRequest>();
static final int MAXQUEUE = 5;
ApiRequest mApi;
public void run() {
try {
while (true) {
getMessage();
}
}
catch(ErrorException e) { }
}
private synchronized void getMessage() throws ErrorException{
try {
notify();
Log.d("DOWNLOADER", "empiezo a coger los mensajes");
while (messages.size() == 0)
wait();
DownloadRequest dr = messages.firstElement();
mApi.setMethod(dr.getRequest());
try {
mApi.executeRequest();
} catch (ErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object o = dr.getObject();
o.notify();
return;
}catch(InterruptedException e) {
throw new ErrorException();
}
Le but est d'attendre de recevoir un nouveau message et appeler l'api.
Cet objet est en cours d'exécution dans un nouveau thread dans cette activité
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
mDownloader = new Downloader(new ApiRequest());
Thread thread = new Thread(mDownloader);
thread.start();
DownloadRequest dr = new DownloadRequest(this,TRIPS_METHOD);
try {
mDownloader.putMessage(dr);
Mais quand thread.start()
est appelée l'interface utilisateur reste bloqué. Je pense que cela ne devrait pas arriver parce que c'est un nouveau sujet.
Quelqu'un peut-il m'aider?
Merci