I wrote a simple program which SHOULD just show the battery level.
But it does not work, and I can't understand why. I always obtain the same value, 0x7fff.ffff (2147486347).Why? Which initialization do I lack?
Is there an alternate method to read the battery level?
The battery indicator on my UIQ phone is ugly, it just shows if it is full, empty, 1/3 or 2/3 charged!
This is the source; I tried to use both com.symbian.javax.power.monitor and just javax.power.monitos, without any success.
Code:import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.power.monitor.*; import com.symbian.javax.power.monitor.*; public class jbattery extends Frame implements ActionListener, PowerMonitorListener { private Label lab1,lab2; private Button exitButton, goButton; private PowerMonitor pMon = null; private EpocHalPowerMonitor epoc = null; public jbattery(){ super("Battery Check"); int remaining, level; this.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); initGui(); pMon = PowerMonitor.getInstance(); epoc = new EpocHalPowerMonitor(); epoc.startEventServer(); // Attacco il listener pMon.addPowerMonitorListener(this); setSize(200,280); updateInfo(); setVisible(true); } private void initGui(){ setLayout(null); lab1 = new Label("%"); lab2=new Label("secs"); exitButton = new Button("Exit"); goButton = new Button("Update"); //******** Set controls positions. lab1.setBounds(10 , 30 , 150, 20); lab2.setBounds(10 , 50 , 150, 20); exitButton.setBounds(70,250,50,20); exitButton.addActionListener(this); goButton.setBounds(145,70,50,20); goButton.addActionListener(this); //******** Add control to frame. add(lab1); add(lab2); add(exitButton); add(goButton); } // Re-fattorizzazione e miglioramenti private void updateInfo(){ int remaining,level; int hour, min, sec; if (pMon!=null){ //remaining = pMon.getEstimatedSecondsRemaining(); // level = pMon.getBatteryLevel(); remaining = epoc.getEstimatedSecondsRemaining(); level = epoc.getBatteryLevel(); hour = remaining /3600; min = (remaining % 3600) /60; sec = remaining - hour*3600 - min*60; lab1.setText( remaining+ " - "+hour+" h "+min+" min "+ sec +" sec left."); // oppure se non funziona la riga sopra sostituiscila con questa sotto // lab1.setText(new Integer(remaining).toString()+" estimated time left."); lab2.setText(new Integer(level).toString()+"% charge level."); }else{ lab1.setText("Estimated time left: n/a"); lab2.setText("Charge level: n/a"); } } // implementazione dell'interfaccia ActionListener public void actionPerformed(ActionEvent evt) { if (evt.getSource().equals(goButton)){ updateInfo(); } if (evt.getSource().equals(exitButton)) { System.exit(0); } } // implementazione dell'interfaccia PowerMonitoListener public void powerWarning(int estimatedSecondsRemaining, PowerWarningType warningType) { if (warningType == PowerWarningType.BatteryLow){ lab1.setText("Batteria bassa!"); } if (warningType == PowerWarningType.ExternalPowerSourceChange){ lab1.setText("In fase di ricarica"); } } public static void main(String[] args){ new jbattery(); } } // program end.

Reply With Quote
Are you sure that this is correct power?

