android内存读取
⑴ android 怎么获取手机内存里的音乐信息
Android自带的音乐播放器中,在获取音乐文件信息的时候是通过扫描得到相关信息的。扫描时使用扫描器MediaScanner完成。
Android系统提供了MediaScanner、MediaProvider、MediaStore等接口,并且提供了一套数据库表格,通过Content Provider的方式提供给用户。当手机开机或者有SD卡插拔等事件发生时,系统将会自动扫描SD卡和手机内存上的媒体文件,如audio、video、图片等,将相应的信息放到定义好的数据库表格中。在这个程序中,我们不需要关心如何去扫描手机中的文件,只要了解如何查询和使用这些信息就可以了。
MediaStore中定义了一系列的数据表格,通过Android ContentResolver提供的查询接口,我们可以得到各种需要的信息。下面我们重点介绍查询SD卡上的音乐文件信息。
先来了解一下ContentResolver的查询接口:
Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Uri:指明要查询的数据库名称加上表的名称,从MediaStore中我们可以找到相应信息的参数。
Projection: 指定查询数据库表中的哪几列,返回的游标中将包括相应的信息。Null则返回所有信息。
selection: 指定查询条件
selectionArgs:参数selection里有 ?这个符号是,这里可以以实际值代替这个问号。如果selection这个没有?的话,那么这个String数组可以为null。
SortOrder:指定查询结果的排列顺序
下面的命令将返回所有在外部存储卡上的音乐文件的信息:
Cursor cursor = query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
得到cursor后,我们可以调用Cursor的相关方法具体的音乐信息:
歌曲ID:MediaStore.Audio.Media._ID
Int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
歌曲的名称:MediaStore.Audio.Media.TITLE
String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
歌曲的歌手名:MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
歌曲文件的路径:MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
歌曲的总播放时长:MediaStore.Audio.Media.DURATION
Int ration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
歌曲文件的大小:MediaStore.Audio.Media.SIZE
Int size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
⑵ android 镐庢牱銮峰彇褰揿墠apk镓鍗犵敤镄勫唴瀛
杩欎釜鏂规硶链夊緢澶氾纴甯哥敤镄勬槸 adb shell mpsys meminfo <package_name> 杩椤彲浠ョ湅鍒版瘆杈冨叏闱㈢殑淇℃伅,鐢变簬Android鏄链夊唴瀛桦叡浜镄勶纴镓浠ラ氩父链 VSS锛孯SS锛孭SS锛孶SS绛変笉钖岀殑鍐呭瓨琛ㄨ堪锛屾瘆杈冨父鐢ㄧ殑鏄疨SS锛屼细灏嗗叡浜搴撴寜镦ф瘆渚嫔垎閰岖粰褰揿墠鍐呭瓨
⑶ 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銮峰彇搴旂敤鍐呭瓨浣跨敤𨱍呭喌镄勬柟娉
1銆 璇ヤ唬镰佽幏鍙栧綋鍓嶅簲鐢ㄧ▼搴忕殑鍐呭瓨浣跨敤𨱍呭喌ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); //链澶у垎閰嶅唴瀛 int memory = activityManager銆
getMemoryClass(); System銆俹ut銆俻rintln("memory: "+memory); //链澶у垎閰嶅唴瀛樿幏鍙栨柟娉2 float maxMemory = (float) (Runtime銆俫etRuntime()銆俶axMemory() * 1銆
0/ (1024 * 1024)); //褰揿墠鍒嗛厤镄勬诲唴瀛 float totalMemory = (float) (Runtime銆俫etRuntime()銆伥otalMemory() * 1銆0/ (1024 * 1024)); //鍓╀綑鍐呭瓨 float freeMemory = (float) (Runtime銆
getRuntime()銆俧reeMemory() * 1銆0/ (1024 * 1024)); System銆俹ut銆俻rintln("maxMemory: "+maxMemory); System銆俹ut銆俻rintln("totalMemory: "+totalMemory); System銆
out銆俻rintln("freeMemory: "+freeMemory);缁撴灉System銆俹ut: memory: 256System銆俹ut: maxMemory: 256銆0System銆俹ut: totalMemory: 11銆974937System銆
out: freeMemory: 3銆6257935杩栾〃鏄庢垜镄勫簲鐢ㄧ▼搴忓湪褰揿墠镓嬫満涓婄殑链澶у垎閰嶅唴瀛树负256m锛岀幇鍦ㄥ凡鍒嗛厤11m锛岃11m涓镄6m鏄鍏嶈垂镄勫綋铹讹纴鎭ㄥ彲浠ラ氲繃Monitors镟寸洿瑙傚湴镆ョ湅鍐呭瓨浣跨敤𨱍呭喌2銆 浣跨敤dos锻戒护锛1锛夋墦寮dos绐楀彛骞舵墽琛宎db shell锛2锛塪umpsys meminfo杞浠跺寘钖岖О缁撴灉:3銆
浣跨敤鏄剧ず鍣ㄦ垨DDMS鏄剧ず鍣―DMS銆
⑸ 如何获取android系统总的内存容量
在设置-存储那里就可以看到了,会显示存储设置的SD卡和内部存储空间的容量,要是运存的话可以看看设置-关于手机那里