当前位置:首页 » 安卓系统 » 安卓手机编程如何获取电池信息

安卓手机编程如何获取电池信息

发布时间: 2023-08-25 10:17:26

❶ Android获取系统cpu信息,内存,版本,电量等信息

1、CPU频率,CPU信息:/proc/cpuinfo和/proc/stat

通过读取文件/proc/cpuinfo系统CPU的类型等多种信息。

读取/proc/stat 所有CPU活动的信息来计算CPU使用率

下面我们就来讲讲如何通过代码来获取CPU频率:

复制代码 代码如下:

package com.orange.cpu;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

public class CpuManager {

// 获取CPU最大频率(单位KHZ)

// "/system/bin/cat" 命令行

// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的.路径

public static String getMaxCpuFreq() {

String result = "";

ProcessBuilder cmd;

try {

String[] args = { "/system/bin/cat",

"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[24];

while (in.read(re) != -1) {

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

result = "N/A";

}

return result.trim();

}

// 获取CPU最小频率(单位KHZ)

public static String getMinCpuFreq() {

String result = "";

ProcessBuilder cmd;

try {

String[] args = { "/system/bin/cat",

"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[24];

while (in.read(re) != -1) {

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

result = "N/A";

}

return result.trim();

}

// 实时获取CPU当前频率(单位KHZ)

public static String getCurCpuFreq() {

String result = "N/A";

try {

FileReader fr = new FileReader(

"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");

BufferedReader br = new BufferedReader(fr);

String text = br.readLine();

result = text.trim();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

// 获取CPU名字

public static String getCpuName() {

try {

FileReader fr = new FileReader("/proc/cpuinfo");

BufferedReader br = new BufferedReader(fr);

String text = br.readLine();

String[] array = text.split(":s+", 2);

for (int i = 0; i < array.length; i++) {

}

return array[1];

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

2、内存:/proc/meminfo

复制代码 代码如下:

public void getTotalMemory() {

String str1 = "/proc/meminfo";

String str2="";

try {

FileReader fr = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(fr, 8192);

while ((str2 = localBufferedReader.readLine()) != null) {

Log.i(TAG, "---" + str2);

}

} catch (IOException e) {

}

}

3、Rom大小

复制代码 代码如下:

public long[] getRomMemroy() {

long[] romInfo = new long[2];

//Total rom memory

romInfo[0] = getTotalInternalMemorySize();

//Available rom memory

File path = Environment.getDataDirectory();

StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();

long availableBlocks = stat.getAvailableBlocks();

romInfo[1] = blockSize * availableBlocks;

getVersion();

return romInfo;

}

public long getTotalInternalMemorySize() {

File path = Environment.getDataDirectory();

StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

return totalBlocks * blockSize;

}

4、sdCard大小

复制代码 代码如下:

public long[] getSDCardMemory() {

long[] sdCardInfo=new long[2];

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

File sdcardDir = Environment.getExternalStorageDirectory();

StatFs sf = new StatFs(sdcardDir.getPath());

long bSize = sf.getBlockSize();

long bCount = sf.getBlockCount();

long availBlocks = sf.getAvailableBlocks();

sdCardInfo[0] = bSize * bCount;//总大小

sdCardInfo[1] = bSize * availBlocks;//可用大小

}

return sdCardInfo;

}

5、电池电量

复制代码 代码如下:

private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){

@Override

public void onReceive(Context context, Intent intent) {

int level = intent.getIntExtra("level", 0);

// level加%就是当前电量了

}

};

registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

6、系统的版本信息

复制代码 代码如下:

public String[] getVersion(){

String[] version={"null","null","null","null"};

String str1 = "/proc/version";

String str2;

String[] arrayOfString;

try {

FileReader localFileReader = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(

localFileReader, 8192);

str2 = localBufferedReader.readLine();

arrayOfString = str2.split("s+");

version[0]=arrayOfString[2];//KernelVersion

localBufferedReader.close();

} catch (IOException e) {

}

version[1] = Build.VERSION.RELEASE;// firmware version

version[2]=Build.MODEL;//model

version[3]=Build.DISPLAY;//system version

return version;

}

7、mac地址和开机时间

复制代码 代码如下:

public String[] getOtherInfo(){

String[] other={"null","null"};

WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

if(wifiInfo.getMacAddress()!=null){

other[0]=wifiInfo.getMacAddress();

} else {

other[0] = "Fail";

}

other[1] = getTimes();

return other;

}

private String getTimes() {

long ut = SystemClock.elapsedRealtime() / 1000;

if (ut == 0) {

ut = 1;

}

int m = (int) ((ut / 60) % 60);

int h = (int) ((ut / 3600));

return h + " " + mContext.getString(R.string.info_times_hour) + m + " "

+ mContext.getString(R.string.info_times_minute);

}

❷ 怎样查看Android手机电池信息

首先进入手机操作系统的拨号“Dialer”界面。
方法一:桌面→联系人→选择拨号盘,进入手机操作系统的拨号“Dialer”界面。

方法二:通过所有应用程序界面选择拨号盘,进入手机操作系统的拨号“Dialer”界面。

输入引号内的字符“*#*#4636#*#*”即可进入Android的工程测试模式即“测试”。

在菜单中有手机信息、电池信息、使用情况统计数据、WLAN information四个选项(各不同版本的ROM可能在表述上存在差异)。

点击第二项电池信息进入,就可以看到目前电池的使用状态了,包括电量等级、电池状态、温度、电池材质、电压等等信息。

安卓手机怎么查看电池寿命

在拨号界面依次输入*#*#4636#*#*即可进入工程模式,在菜单中有手机信息电池信息,使用情况统计数据,WLANinformatation四个选项,点击电池信息进入,就可以看到目前电池的使用状态了。

或是下载如下图所示的软件,也可进行查看电池使用情况。

拓展资料

电池损耗指的是电池在长时间使用后,实际容量变小,低于标称容量。电池损耗通称为记忆效应,但一般仅指笔记本电脑的记忆效应。电池损耗的原因很多,主要包括:

1、电池本身质量较差

2、使用劣质充电器

3、高温或低温环境长时间作业

4、使用电池是接高负荷外设,插拔不规范。

5、过度充放电(电池使用到剩余电量低于3%)。电路都有保护,但是偶尔会发生。

6、不完全充放电(没有做到用完再充,而是想用就用,想充就充)

7、长期在电磁强辐射地使用电池(会干扰电池内金属的导电性)

8、长期在湿润的条件下使用电池(潮湿的电池会性能降低)

❹ 有没有什么方法能够快速,实时的获取android手机的电量值

这个是获取android电量的DEMO:

package com.android.batterywaster;

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.os.PowerManager;

import android.view.View;

import android.widget.CheckBox;

import android.widget.TextView;

import java.text.DateFormat;

import java.util.Date;

/**

* So you thought sync used up your battery life.

*/

public class BatteryWaster extends Activity {

TextView mLog;

DateFormat mDateFormat;

IntentFilter mFilter;

PowerManager.WakeLock mWakeLock;

SpinThread mThread;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Set the layout for this activity. You can find it

// in res/layout/hello_activity.xml

setContentView(R.layout.main);

findViewById(R.id.checkbox).setOnClickListener(mClickListener);

mLog = (TextView)findViewById(R.id.log);

mDateFormat = DateFormat.getInstance();

mFilter = new IntentFilter();

mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);

mFilter.addAction(Intent.ACTION_BATTERY_LOW);

mFilter.addAction(Intent.ACTION_BATTERY_OKAY);

mFilter.addAction(Intent.ACTION_POWER_CONNECTED);

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);

mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster");

mWakeLock.setReferenceCounted(false);

}

@Override

public void onPause() {

stopRunning();

}

View.OnClickListener mClickListener = new View.OnClickListener() {

public void onClick(View v) {

CheckBox checkbox = (CheckBox)v;

if (checkbox.isChecked()) {

startRunning();

} else {

stopRunning();

}

}

};

void startRunning() {

log("Start");

registerReceiver(mReceiver, mFilter);

mWakeLock.acquire();

if (mThread == null) {

mThread = new SpinThread();

mThread.start();

}

}

void stopRunning() {

log("Stop");

unregisterReceiver(mReceiver);

mWakeLock.release();

if (mThread != null) {

mThread.quit();

mThread = null;

}

}

void log(String s) {

mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);

}

BroadcastReceiver mReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

String title = action;

int index = title.lastIndexOf('.');

if (index >= 0) {

title = title.substring(index + 1);

}

if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {

int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

int icon = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,-1);

log(title + ": level=" + level + "\n" + "icon:" + icon);

} else {

log(title);

}

}

};

class SpinThread extends Thread {

private boolean mStop;

public void quit() {

synchronized (this) {

mStop = true;

}

}

public void run() {

while (true) {

synchronized (this) {

if (mStop) {

http://www.dewen.io/q/1428

wipe电池如下:
1、把电池充满,充满满的,在开机状态下冲到满,然后拔下充电器,手机关机,再插上充电器充电,直至再次显示充满,拔下充电器,开机再充电,这样反复几次就能把电池充到尽量满的状态。
2、然后关机,开机进入Recovery模式。
选择advanced(高级功能) -> wipe battery stats(清空电池状态)->yes(是)
然后++++ Go Back(返回上级) ++++ --> - reboot system now(重启系统) -

热点内容
phppdf转html 发布:2025-03-09 08:23:01 浏览:639
脚本按键怎么循环 发布:2025-03-09 08:19:06 浏览:142
intel的快速存储 发布:2025-03-09 08:18:25 浏览:609
ios天天酷跑刷积分脚本 发布:2025-03-09 08:12:01 浏览:736
2nf算法 发布:2025-03-09 08:05:15 浏览:931
一体机安卓系统怎么登 发布:2025-03-09 08:00:48 浏览:936
safemon是什么文件夹 发布:2025-03-09 07:47:03 浏览:819
ipa反编译源码 发布:2025-03-09 07:41:06 浏览:295
电脑xp密码忘了怎么办 发布:2025-03-09 07:38:18 浏览:828
联想云控制台只能一个服务器吗 发布:2025-03-09 07:38:16 浏览:584