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

Composants graphiques Android Discussion :

Comment afficher les informations exactes de la batterie dans un TextView


Sujet :

Composants graphiques Android

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 104
    Par défaut Comment afficher les informations exactes de la batterie dans un TextView
    Bonsoir à tous!

    Voilà je voudrais savoir comment afficher les informations de la batterie.

    Voici mon code Java :
    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
     
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
     
    public class InfosBatterie extends Activity {
     
    	private int level;
    	private int maxValue;
    	private int pctCharge;
    	private int health;
    	private int status;
    	private int technology;
    	private int temperature;
    	private int voltage;
     
     
     
     
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
     
            LayoutInflater inflater = getLayoutInflater();
        	View batterie_layout = inflater.inflate(R.layout.infos_batterie,(ViewGroup) findViewById(R.id.batterie_layout));
     
        	Typeface font = Typeface.createFromAsset(getAssets(), "HollaScript.ttf");
     
        	ImageView imgBat1 = (ImageView)batterie_layout.findViewById(R.id.imgBat1);
        	imgBat1.setImageResource(R.drawable.batterie_info);
     
        	TextView tvBat1 = (TextView)batterie_layout.findViewById(R.id.tvBat1);
        	tvBat1.setText("Santé : " +health);
        	tvBat1.setTypeface(font);
     
        	TextView tvBat2 = (TextView)batterie_layout.findViewById(R.id.tvBat2);
        	tvBat2.setText("Status : " +status);
        	tvBat2.setTypeface(font);
     
        	TextView tvBat3 = (TextView)batterie_layout.findViewById(R.id.tvBat3);
        	tvBat3.setText("Tension : " +voltage);
        	tvBat3.setTypeface(font);
     
        	TextView tvBat4 = (TextView)batterie_layout.findViewById(R.id.tvBat4);
        	tvBat4.setText("Technologie : " +technology);
        	tvBat4.setTypeface(font);
     
        	TextView tvBat5 = (TextView)batterie_layout.findViewById(R.id.tvBat5);
        	tvBat5.setText("Température : " +temperature+ "°C");
        	tvBat5.setTypeface(font);
     
        	TextView tvBat6 = (TextView)batterie_layout.findViewById(R.id.tvBat6);
        	tvBat6.setText("Niveau de charge : " +pctCharge+ " %");
        	tvBat6.setTypeface(font);
     
            setContentView(batterie_layout);
     
        }
     
    @Override
    protected void onStart() {
    	this.registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    	super.onStart();
    }
     
    @Override
    protected void onStop() {
    	this.unregisterReceiver(receiver);
    	super.onStop();
    }
     
    private BroadcastReceiver receiver = new BroadcastReceiver(){
     
    	@Override
    	public void onReceive(Context context, Intent intent) {
     
    		level = intent.getIntExtra("level", -1);
    		maxValue= intent.getIntExtra("scale",-1);
    		pctCharge = (level * 100)/maxValue;
     
    		health = intent.getIntExtra("health", -1);
    		status = intent.getIntExtra("status", -1);
    		voltage = intent.getIntExtra("voltage", -1);
    		temperature = intent.getIntExtra("temperature", -1);
    		technology = intent.getIntExtra("technology", -1);
     
    	}};}
    Mon Problème est que les valeurs affichées sont toutes égales à 0..

    Donc j'aurai besoin de vôtre aide pour afficher les valeurs réelles.

    Merci à vous !

  2. #2
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Salut,

    Je n'ai jamais été amené à faire ça, donc je saurais peut être pas t'aider, mais n'y a-t'il pas une permission à rajouter dans le manifest pour avoir accès à ce genre d'information ?

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 104
    Par défaut
    D'abord, merci de prêter attention à mon post

    Pour ce qui est de la permission, il me semble que c'est celle-ci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
    Je l'ai bien déclarée dans mon Manifest mais toujours le même soucis...
    Toutes les valeurs que je souhaite afficher sont égales à 0.

  4. #4
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Et tu travailles sur l'émulateur ou sur un vrai téléphone ?

  5. #5
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Salut,


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     getApplicationContext().(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    au lieu de

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    N'oublie pas remettre à jour l'affichage lorsque tu reçoit la notification des nouvelles stats de la batterie


    http://stackoverflow.com/questions/3...ion-battery-ch

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 104
    Par défaut
    Citation Envoyé par MrDuChnok Voir le message
    Et tu travailles sur l'émulateur ou sur un vrai téléphone ?
    Sur un vrai téléphone.


    Salut,


    Code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    getApplicationContext().(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    au lieu de

    Code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    this.registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    N'oublie pas remettre à jour l'affichage lorsque tu reçoit la notification des nouvelles stats de la batterie


    http://stackoverflow.com/questions/3...ion-battery-ch
    Merci je vais voir ça de plus près

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 104
    Par défaut
    Bon alors j'ai repris tout mon code et je l'ai modifié en cela :
    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
     
    public class InfosBatterie extends Activity {
     
            private TextView tvBat1;
            private TextView tvBat2;
            private TextView tvBat3;
            private TextView tvBat4;
            private TextView tvBat5;
            private TextView tvBat6;        
     
            private BroadcastReceiver receiver = new BroadcastReceiver(){
     
     
                    @Override
                    public void onReceive(Context arg0, Intent intent) {
     
                            int health              = intent.getIntExtra("health", 0);
                            tvBat1.setText("Santé de la batterie : " +String.valueOf(health));
     
                            int status              = intent.getIntExtra("status", 0);
                            tvBat2.setText("Status de la batterie : " +String.valueOf(status));
     
                            int voltage     = intent.getIntExtra("voltage", 0);
                            tvBat3.setText("Tension de la batterie : " +String.valueOf(voltage)+ " mV");
     
                            int technology = intent.getIntExtra("technology", 0);
                            tvBat4.setText("Technologie de la batterie : " +String.valueOf(technology));
     
                            int temperature         = intent.getIntExtra("temperature", 0);
                            tvBat5.setText("Température de la batterie : " +String.valueOf(temperature));
     
                            int level               = intent.getIntExtra("level", 0);
                            tvBat6.setText("Niveau de charge de la batterie : " +String.valueOf(level)+ " %");
     
                    }};
     
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
     
           LayoutInflater inflater = getLayoutInflater();
               View batterie_layout = inflater.inflate(R.layout.infos_batterie,(ViewGroup) findViewById(R.id.batterie_layout));     
           setContentView(batterie_layout);
     
     
            Typeface font = Typeface.createFromAsset(getAssets(), "HollaScript.ttf");
     
            ImageView imgBat1 = (ImageView)batterie_layout.findViewById(R.id.imgBat1);
            imgBat1.setImageResource(R.drawable.batterie_info);
     
            tvBat1 = (TextView)batterie_layout.findViewById(R.id.tvBat1);
            tvBat1.setTypeface(font);
     
            tvBat2 = (TextView)batterie_layout.findViewById(R.id.tvBat2);
            tvBat2.setTypeface(font);
     
            tvBat3 = (TextView)batterie_layout.findViewById(R.id.tvBat3);
            tvBat3.setTypeface(font);
     
            tvBat4 = (TextView)batterie_layout.findViewById(R.id.tvBat4);
            tvBat4.setTypeface(font);
     
            tvBat5 = (TextView)batterie_layout.findViewById(R.id.tvBat5);
            tvBat5.setTypeface(font);
     
            tvBat6 = (TextView)batterie_layout.findViewById(R.id.tvBat6);           
            tvBat6.setTypeface(font);
     
            this.registerReceiver(this.receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     
     
        }}

    Par contre, j'ai un petit soucis.
    Les affichages suivant me retourne une valeur entière :

    health : 2
    status : 4 (quand mon téléphone n'est pas en charge)
    technology : 0
    plugged : 0

    Comment afficher du texte correspondant à ces chiffres?

    Je pense qu'il faut que j'utilise les

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    public static final int BATTERY_HEALTH_GOOD
    etc....

    mais je ne sais pas comment faire correspondre ces valeurs à du texte.
    Genre health = 2 où 2 égal Bonne.
    Je ne sais pas si c'est compréhensible et j'en suis désolé.
    Si vous pouvais m'éclairer...?

    Merci pour vos réponses !

  8. #8
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Salut,
    Par contre, j'ai un petit soucis.
    Les affichages suivant me retourne une valeur entière :
    Déjà premier point cela est normal que tu es des valeurs entières puisque tu les récupère et stocke via un int, ce qui est logique .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     int health              = intent.getIntExtra("health", 0);
                            tvBat1.setText("Santé de la batterie : " +String.valueOf(health));
    Comment afficher du texte correspondant à ces chiffres?
    Pöur cela il suffit de connaître à quoi correspondent ces valeurs et de faire une correspondance dessus .
    Je te met un lien sur les la class Battery

    http://hi-android.info/src/android/o...ager.java.html

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 104
    Par défaut
    Merci Fenaorin !

    Mon problème est totalement réglé !

    voici une partie du code si cela peut aider :
    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
     
    public class InfosBatterie extends Activity {
     
    	private TextView tvBat1;
    	private TextView tvBat2;
    	private TextView tvBat3;
    	private TextView tvBat4;
    	private TextView tvBat5;
    	private TextView tvBat6;
    	private TextView tvBat7;
     
    	private ImageView imgBat1;
     
    	private BroadcastReceiver receiver = new BroadcastReceiver(){
     
     
    		@Override
    		public void onReceive(Context arg0, Intent intent) {
     
    			int health 		= intent.getIntExtra("health", 0);
    			tvBat1.setText("Santé de la batterie : " +String.valueOf(health));
    			if (health == BatteryManager.BATTERY_HEALTH_GOOD)
                {
    				tvBat1.setText("Santé de la batterie : "+" Bonne");
                }
                else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT)
                {
                	tvBat1.setText("Santé de la batterie : "+" En surchauffe");
                }
                else if (health == BatteryManager.BATTERY_HEALTH_DEAD)
                {
                	tvBat1.setText("Santé de la batterie : "+" Morte");
                }
                else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE)
                {
                	tvBat1.setText("Santé de la batterie : "+" Surtension");
                }
                else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE)
                {
                	tvBat1.setText("Santé de la batterie : "+" Erreur non spécifiée");
                }
                else
                {
                	tvBat1.setText("Santé de la batterie : "+" Inconnue");
                	imgBat1.setImageResource(R.drawable.batterie_unknow);
                }
    }};
     
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
     
           LayoutInflater inflater = getLayoutInflater();
       	   View batterie_layout = inflater.inflate(R.layout.infos_batterie,(ViewGroup) findViewById(R.id.batterie_layout));   	
           setContentView(batterie_layout);
     
     
        	Typeface font = Typeface.createFromAsset(getAssets(), "HollaScript.ttf");
     
        	imgBat1 = (ImageView)batterie_layout.findViewById(R.id.imgBat1);
        	imgBat1.setImageResource(R.drawable.batterie_info);
     
        	tvBat1 = (TextView)batterie_layout.findViewById(R.id.tvBat1);
        	tvBat1.setTypeface(font);
     
    this.registerReceiver(this.receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     
     
        }};


    Je vais profiter de ce post pour vous posez une autre question du même genre.

    Je voudrais maintenant afficher les informations relatives aux différentes mémoires pour les afficher dans une progressBar.

    Suite ici :
    http://www.developpez.net/forums/d10...oire-textview/

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 07/02/2012, 10h17
  2. Comment afficher les resultats d'un scan Wifi dans une ListView?
    Par xXO--Hades--OXx dans le forum Composants graphiques
    Réponses: 8
    Dernier message: 09/02/2011, 14h14
  3. Comment afficher les informations exactes de la mémoire dans un TextView
    Par xXO--Hades--OXx dans le forum Composants graphiques
    Réponses: 5
    Dernier message: 24/01/2011, 23h24
  4. Réponses: 9
    Dernier message: 31/05/2009, 04h59
  5. Réponses: 5
    Dernier message: 04/04/2008, 21h14

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