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 :

Comment exécuter une procédure au lancement de l'Activity


Sujet :

Android

  1. #1
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2014
    Messages : 93
    Points : 166
    Points
    166
    Par défaut Comment exécuter une procédure au lancement de l'Activity
    Bonjour,
    Le but de ce mini-programme est d'imprimer un fichier sur l'imprimante intégrée au smartphone.
    J'ai donc réussi tant bien que mal à modifier le seul exemple de code java, pour arriver à faire ce dont j'ai besoin.

    Le principe c'est qu'une application A (Écrite en Windev Mobile) génère un fichier texte puis lance l'application B (écrite en java avec Eclipse)
    Pour le moment, quant l'application B prend le focus, un bouton apparait et quant j'appuie dessus l'impression se fait et l'Activity est fermée rendant la main à l'application A. Le process "manuel" fonctionne .

    Maintenant j'aimerais ne plus à avoir à cliquer sur ce bouton... et là je sèche

    J'ai testé l'appel à PrintData()
    - après init(); dans le onCreate : rien ne s'imprime, pourtant le fichier à imprimer est bien supprimé et je rend bien la main à l'application A.
    - dans onStart : Arrêt brutal de l'application et message : "V5Print" s'est arrêté.

    Mes connaissances en java sont très limitées...
    Merci d'avance à ceux / celles qui m'apporteront leur aide


    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
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
     
    package com.citaq.v5print;
     
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.IBinder;
    import android.os.RemoteException;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import woyou.aidlservice.jiuiv5.ICallback;
    import woyou.aidlservice.jiuiv5.IWoyouService;
     
    //----- start of Activity
    public class MainActivity extends Activity {
     
    	Button bt_printFile;
     
    	String sDataFile;
    	String ligne;
    	String ReadBuffer = "";
    	protected static final String TAG = null;
    	IWoyouService woyouService;
     
    	//------------------------------------------------------------------------- onPause
    	@Override
    	protected void onPause() {
    		super.onStop();
    	}
     
    	//------------------------------------------------------------------------- onCreate
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
     
    		Intent intent = new Intent();
    		intent.setPackage("woyou.aidlservice.jiuiv5");
    		intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
    		startService(intent);
    		bindService(intent, connService, Context.BIND_AUTO_CREATE);
    		init();	
    	}
     
     
    	//------------------------------------------------------------------------- onStart
    	// Arret brutal de l'application et message : "V5Print" s'est arrêté.
    	//-------------------------------------------------------------------------
    	//@Override
    	//protected void onStart() {
    	//	PrintData();
    	//}
     
    	//-------------------------------------------------------------------------Impression
    	private void PrintData() {
    		File sdLien = Environment.getExternalStorageDirectory();
            File monFichier = new File(sdLien + "/echange.txt");
     
            if ( monFichier.exists()) {
            	try {
            		//----- Lecture du fichier d'échange
            		BufferedReader fichier = new BufferedReader(new FileReader(monFichier));
            		while ((ligne = fichier.readLine()) != null) {
            			ReadBuffer = ReadBuffer + ligne + "\n";
            			}
            		//----- Fermeture et suppression du fichier d'échange
            		fichier.close();
            		monFichier.delete();
     
            		} catch (Exception e) {
            			e.printStackTrace();
            			}
     
            	//-----Impression
            	Toast.makeText(getApplicationContext(), "Impression en cours...", Toast.LENGTH_SHORT).show();
    			try {
    				woyouService.printText(ReadBuffer + "\n", callback);
    		        //----- Bye Bye
    		        System.exit(0);
     
    			} catch (RemoteException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
     
            }	// End if
     
           	System.exit(0);
    	}
     
    	//------------------------------------------------------------------------- Init
    	private void init(){
    		bt_printFile = (Button) findViewById(R.id.bt_PrintFile);
     
    		//--------------------------------------------------------------------- Bouton printFile
    		bt_printFile.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View arg0) {
    				PrintData();
    		   }		// End onClick
    		});		// End bt_printFile.setOnClickListener
    	}	// end of init()
     
    	//-------------------------------------------------------------------------
    	private ServiceConnection connService = new ServiceConnection() {
    		@Override
    		public void onServiceDisconnected(ComponentName name) {
    			woyouService = null;
    		}
    		@Override
    		public void onServiceConnected(ComponentName name, IBinder service) {
    			woyouService = IWoyouService.Stub.asInterface(service);
     
    			try {
    				woyouService.printerInit(callback);
     
    			} catch (RemoteException e) {
    				Log.d(TAG, "registerCallback failed.");
    			}
     
    		}
    	};	// End connService
     
    	//-------------------------------------------------------------------------
    	private ICallback callback = new ICallback.Stub() {
     
    		@Override
    		public void onRunResult(boolean isSuccess) throws RemoteException {
    			Log.d(TAG, "ICallback--->" + isSuccess); 
    		}
     
    		@Override
    		public void onReturnString(String result) throws RemoteException {
    			Log.d(TAG, "ICallback--->" + result); 
    		}
     
    		@Override
    		public void onRaiseException(int code, String msg) throws RemoteException {
    			Log.d(TAG, "onRaiseException--->" + msg);
    		}		
     
    	};	// End callback
     
    }	// End Activity

  2. #2
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    finish()

    au lieu de System.exit() qui est à ne *jamais* utiliser en java.
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2014
    Messages : 93
    Points : 166
    Points
    166
    Par défaut
    Suite...
    Afin de comprendre pourquoi cela ne fonctionnait pas en mettant l'appel à PrintData() à la fin du onCreate(), j'ai mis des Log.d() quasiment à chaque ligne
    et le résultat donne :
    ///// AVEC LE BOUTON
    D/V5Print (18685): Start of OnCreate()
    D/V5Print (18685): Start of init()
    D/V5Print (18685): End of init()
    D/V5Print (18685): End of OnCreate()
    D/V5Print (18685): onServiceConnected()
    D/V5Print (18685): woyouService.printerInit
    D/V5Print (18685): onReturnString-ICallback--->733
    D/V5Print (18685): onRunResult-ICallback--->true
    ......... Attente du Clic sur le bouton "Print"...........
    D/V5Print (18685): Execution de PrintData() dans onClick()
    D/V5Print (18685): Start of PrintData()
    D/V5Print (18685): echange.txt found.
    D/V5Print (18685): OPEN echange.txt
    D/V5Print (18685): CLOSE echange.txt
    D/V5Print (18685): DELETE echange.txt
    D/V5Print (18685): Start of print.
    D/V5Print (18685): Ceci est un test...
    D/V5Print (18685): Le 28/11/2016
    D/V5Print (18685): à 13:13:01
    D/V5Print (18685): ligne 01 a A
    D/V5Print (18685): ligne 02 b B
    D/V5Print (18685): ligne 03 c C
    D/V5Print (18685): ligne 04 d D
    D/V5Print (18685): ligne 05 e E
    D/V5Print (18685): ligne 06 f F
    D/V5Print (18685): End of print. Bye Bye

    ///// AVEC PrintData() à la fin de onCreate()
    D/V5Print (18900): Start of OnCreate()
    D/V5Print (18900): Start of init()
    D/V5Print (18900): End of init()
    D/V5Print (18900): Execution de PrintData() dans OnCreate()
    D/V5Print (18900): Start of PrintData()
    D/V5Print (18900): echange.txt found.
    D/V5Print (18900): OPEN echange.txt
    D/V5Print (18900): CLOSE echange.txt
    D/V5Print (18900): DELETE echange.txt
    D/V5Print (18900): Start of print.
    D/V5Print (18900): Ceci est un test...
    D/V5Print (18900): Le 28/11/2016
    D/V5Print (18900): à 13:17:50
    D/V5Print (18900): ligne 01 a A
    D/V5Print (18900): ligne 02 b B
    D/V5Print (18900): ligne 03 c C
    D/V5Print (18900): ligne 04 d D
    D/V5Print (18900): ligne 05 e E
    D/V5Print (18900): ligne 06 f F

    // on n'est pas passé par private ServiceConnection connService()
    // ni par private ICallback callback = new ICallback.Stub()
    // ==> Erreur !
    D/V5Print (18900): Erreur Exception woyouService.printText
    D/V5Print (18900): echange.txt not found... Bye Bye
    D/V5Print (18900): End of OnCreate()
    D/V5Print (18900): onServiceConnected()
    D/V5Print (18900): woyouService.printerInit
    D/V5Print (18900): onReturnString-ICallback--->768
    D/V5Print (18900): onRunResult-ICallback--->true

    La question est donc Comment forcer le passage avant de lancer PrintData() ???

  4. #4
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2014
    Messages : 93
    Points : 166
    Points
    166
    Par défaut
    RESOLU
    Il suffisait de mettre l'appel à la fonction PrintData() juste après woyouService.printerInit(callback);

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

Discussions similaires

  1. comment exécuter une procédure stocké en vb.net
    Par ratsmok dans le forum VB.NET
    Réponses: 3
    Dernier message: 06/06/2009, 19h40
  2. Réponses: 14
    Dernier message: 14/01/2009, 15h59
  3. Réponses: 2
    Dernier message: 29/05/2007, 14h50
  4. Réponses: 2
    Dernier message: 20/03/2007, 17h00
  5. Réponses: 1
    Dernier message: 26/07/2006, 11h23

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