PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV Mobile 2024 → Android - Savoir si peripherique est en charge
Android - Savoir si peripherique est en charge
Débuté par Baptiste - BLV TECH, 14 sep. 2017 21:04 - 3 réponses
Membre enregistré
258 messages
Popularité : +35 (37 votes)
Posté le 14 septembre 2017 - 21:04
Bonjour,

Je ne trouve pas de Fonction permettant de savoir si mon périphérique est en charge ou non.

Une idée ?

Merci

--
Baptiste CLOART
Freelance
b.cloart@blv-tech.com
06.88.74.80.65
www.linkedin.com/in/cloart
www.blv-tech.com>
Membre enregistré
179 messages
Popularité : +17 (17 votes)
Posté le 15 septembre 2017 - 12:13
Salut Baptiste,

voici une solution :

Créer une procédure Java avec ce code :
import android.os.BatteryManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public static boolean estEnCharge() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = getContexteApplication().registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
return isCharging;
}


Appeler la procédure dans le code WLangage par, par exemple :
SIestEnCharge()ALORS
ToastAffiche("L'appareil est en charge")
SINON
ToastAffiche("L'appareil n'est pas en charge")
FIN


Trouvé grâce à la doc Android :https://developer.android.com/training/monitoring-device-state/battery-monitoring.htmlet celle de PCSoft (notamment pour la récupération du "Context")https://doc.pcsoft.fr/fr-FR/?9000108#NOTE10_1

Bonne journée et bons devs!

>Jérôme
Membre enregistré
258 messages
Popularité : +35 (37 votes)
Posté le 15 septembre 2017 - 21:51
Super merci Jérôme pour le code et pour les sources.

--
Baptiste CLOART
Freelance
b.cloart@blv-tech.com
06.88.74.80.65
www.linkedin.com/in/cloart
www.blv-tech.com>
Posté le 18 septembre 2017 - 09:20
Bonjour,

Dans le cas où, en voici une seconde :

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

public static void LitEtatBatterie(String champDeSaisie) {

contentTxt = (TextView) getView(champDeSaisie);
getActiviteEnCours().registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}


static TextView contentTxt;


static BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);



String sPlugged = "";
if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
sPlugged = "En charge (secteur)";
} else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
sPlugged = "En charge (USB)";
} else if (plugged == 0) {
sPlugged = "Mode batterie";
} else {
sPlugged = "Inconnu";
}

contentTxt.setText(
sPlugged

);
}
};


Par ailleurs, pour restituer l'info dans dans un champ de saisie :

>LitEtatBatterie("Nom_ChampDeSaisie)