• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Laptop Battery

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to know how to get how much power is left in a laptop battery on Windows XP. It needs to be in a percent, or something I can convert to a percent like capacity and current charge. I've done google searches and not found much of any use.

I've done work with Visual Basic in the past and I know that you can call one of the Windows APIs to get all the information about the battery and even the statistics Windows keeps track of for more accurate life time predictions. I'm thinking if there's not a way to do this entirely in Java, maybe I could call the Windows API (I've never done this in Java though) to get the info.

Anybody have any helpful information, pointers, or links?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm certain that you can't do this using only Java. To call a Windows API from Java, you would probably want to use JNI. Alternately if there's a way to do this from a command line, you could use Runtime.getRuntime().exec(command) to execute the command. Then take that Process and use getInputStream() to read whatever the command prints out. Those are the main two techniques for doing non-Java stuff from Java.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have the professional version of XP, you can do a Runtime.exec() of the "typeperf" command. From the command line, you can use the "-qx" switch to find the battery counter. And then do a listing of that counter. There are also modes to print it in different formats, on sampling intervals, etc.

Once you find the exact command that suit your needs, then use Runtime.exec() from Java.

Henry
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmmm...

So let's suppose I wanted to use the 'GetSystemPowerStatus' command out of the 'Kernal32.dll' Windows API (this is the one a Visual Basic programmer would call) How would I get Java to communicate with this C++ library?
 
Jim Yingst
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd want to use JNI - the Wikipedia article I linked to gives a good overview, and has links to additional resources. Basically you'd declare a native method in your Java class:

Note there's no implementation - you'd write that in your C/C++ code. There are a number of details to handle here, so it would be best if you read up on the link provided, and maybe check out some of the other resources. Personally I haven't done this in ages, and don't really remember all the details anyway.
 
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic