IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

thread en background


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2011
    Messages : 17
    Par défaut thread en background
    bonjour,

    je suis entrain de développer une application permettant de faire un backup mobile sur un PC et ce dans le cadre d'un projet de diplôme. le souci est que j'aimerai faire fonctionner un thread dans mon application android en arrière plan. Or une fois ce thread se trouve lancer, c'est l'application android elle même qui s'exécute en arrière plan. En gros l'application reste bloquée sur le thread.

    Ce thread a comme tache d'envoyer une liste de fichiers sur un serveur (PC). Et dans mon application j'aimerai pouvoir afficher grâce a un View de type ProgressBar l'état d'avancement du transfert.

    voici la partie du code de mon application android:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public void BackupMediaMenu(View Button) {
     
    		pictureRadio = (RadioButton) findViewById(R.id.pictureRadioBtn);
    		audioRadio = (RadioButton) findViewById(R.id.audioRadioBtn);
    		videoRadio = (RadioButton) findViewById(R.id.videoRadioBtn);
     
    		SendList sendMList = new SendList(user.userCommand, PICTURE_VIDEO_PATH);
     
    		Thread thread = new Thread(sendMList);
     
    		try {
    			if (pictureRadio.isChecked()) {
    				sendMList.setMediaIndex(0);
    			}
    			if (videoRadio.isChecked()) {
    				sendMList.setMediaIndex(1);
    			}
    			if (audioRadio.isChecked()) {
    				sendMList.setMediaIndex(2);
    			}
    			thread.start();			
    			transfert();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    	}
     
     
    public void transfert() {
    		setContentView(R.layout.progress);
    		ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar2);
    		pb.setMax(nbFiles);
    		int valProg = 0;
    		while (valProg<=nbFiles){
    			if (user.userCommand.getFileFlagTransfert()){
    				pb.setProgress(valProg++);
    			}		
    		}
    	}
    merci d'avance!

  2. #2
    Modérateur
    Avatar de grunk
    Homme Profil pro
    Lead dév - Architecte
    Inscrit en
    Août 2003
    Messages
    6 693
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Lead dév - Architecte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2003
    Messages : 6 693
    Par défaut
    Ta class SendList devrais dériver de Thread si tu veux qu'elle soit indépendante de ton Thread d'interface.
    Dans le code que tu as donné je vois pas bien ce que tu cherche à faire.
    Pry Framework php5 | N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Rédacteur
    Avatar de Viish
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2009
    Messages
    427
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2009
    Messages : 427
    Par défaut
    Je suppose que sendMList hérite de Runnable ? Si c'est bien le cas, voici comment faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    final ProgressDialog mProgress = new ProgressDialog(this);
    mProgress.setTitle("Patientez s'il vous plait");
    mProgress.setOnDismissListener(new OnDismissListener() 
    {
    	public void onDismiss(DialogInterface dialog) 
    	{
    		try
    		{
    			Log.d("DEBUG", "Traitement fini");
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    });
    new Thread(sendMList).start();
    mProgress.show();
    Attention, il faut qu'à la fin, ton Runnable sendMList appelle mProgress.dismiss(); !

  4. #4
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2011
    Messages : 17
    Par défaut
    Citation Envoyé par grunk Voir le message
    Ta class SendList devrais dériver de Thread si tu veux qu'elle soit indépendante de ton Thread d'interface.
    Dans le code que tu as donné je vois pas bien ce que tu cherche à faire.
    salut

    En effet SendList dérive de thread. Voici le code de cette classe:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    package UserPackage;
     
    import java.io.File;
     
     
    public class SendList implements Runnable {
     
    	private UserCommand userCmd;
     
    	// path of the dir in the server
    	private String path;
     
    	// number of files received
    	private int nbFileSent;
     
    	// index of media files type 0: image, 1:video, 2: audio
    	private int mediaIndex; 
     
    	/**
             * constructor of RecList to instantiate the class RecList for receiving a
             * list of file
             * 
             * @param path
             * @param listName
             *            the list name of the files to receive
             */
    	public SendList(UserCommand userCmd, String path) {
    		this.path = path;
    		this.userCmd = userCmd;
    	}
     
    	/**
             * to switch which media type file to send 
             * @param index
             */
    	public void setMediaIndex(int index){
    		mediaIndex = index;
    	}
     
    	/**
             * return the number of files existing in the directory given by the path
             * path
             * 
             * @param path
             *            the path containing the files to send
             * @return return the number of file in path
             */
    	public void sendList(String path) {
    		File dir = new File(path);
     
    		if (!dir.isDirectory())
    			return;
     
    		File[] list = dir.listFiles();
    		for (int i = 0; i < list.length; i++) {
    			for (int j = 0; j < UserCommand.EXT[mediaIndex].length; j++){
    				if (list[i].getName().contains(UserCommand.EXT[mediaIndex][j])) {
    					String fileName = list[i].getName()
    							.replace(" ", "_");
    					try {
    						if (userCmd.sendFile(list[i].getAbsolutePath(), fileName) == 1) {
    							nbFileSent++;
    						}
    					} catch (Exception e) {
    						System.err.println("Failed to send: " + fileName);
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    	}
     
    	/**
             * return the number of file received
             * from server 
             * @return
             */
    	public int getNbFileSent(){
    		return nbFileSent;
    	}
     
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		sendList(path);	
    	}
     
    }
    j'aimerai lorsque je lance un thread SendList, qu'il s'exécute en arrière plan. Or moi j'utilise une méthode élémentaire pour l'envoi de fichier (sendFile de la classe UserCommand).

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Un thread en background ou Push Registry?
    Par Mariquiqui dans le forum Développement Mobile en Java
    Réponses: 1
    Dernier message: 11/08/2011, 19h04
  2. Thread + progressbar + background worker
    Par s7even dans le forum Windows Forms
    Réponses: 1
    Dernier message: 21/12/2009, 10h54
  3. Background worker et objets créés à l'interieur du second thread
    Par zax-tfh dans le forum Windows Presentation Foundation
    Réponses: 13
    Dernier message: 16/02/2009, 17h22
  4. Background Thread et ProgressBar
    Par bgcode dans le forum VB.NET
    Réponses: 17
    Dernier message: 05/07/2007, 13h30
  5. arret Background worker thread
    Par ricky78 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 06/02/2007, 12h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo