Android開(kāi)發(fā)中,常常需要獲取本地或者網(wǎng)絡(luò)多媒體的一些基本信息。MediaMetadataRetriever類位于android.media包下,提供了用于從輸入媒體文件檢索幀和元數(shù)據(jù)的統(tǒng)一接口,可以很方便實(shí)現(xiàn)這些功能。

成都創(chuàng)新互聯(lián)專注于城廂企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站定制開(kāi)發(fā)。城廂網(wǎng)站建設(shè)公司,為城廂等地區(qū)提供建站服務(wù)。全流程按需定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
本例中提供了獲取本地視頻和網(wǎng)絡(luò)視頻第一幀圖片的功能,首先初始化MediaMetadataRetriever,如果是本地視頻設(shè)置數(shù)據(jù)源時(shí)通過(guò) Uri.fromFile()轉(zhuǎn)化為Uri對(duì)象,再通過(guò)MediaMetadataRetriever的getFrameAtTime()方法默認(rèn)獲取第一幀圖片。如果是網(wǎng)絡(luò)視頻直接將url設(shè)置為數(shù)據(jù)源即可。
在Android中,想要獲取系統(tǒng)信息,可以調(diào)用其提供的方法System.getProperty(propertyStr),而系統(tǒng)信息諸如用戶根目錄(user.home)等都可以通過(guò)這個(gè)方法獲取,實(shí)現(xiàn)代碼如下:
Java代碼:
public static StringBuffer buffer = null;
private static String initProperty(String description,String propertyStr) {
if (buffer == null) {
buffer = new StringBuffer();
}
buffer.append(description).append(":");
buffer.append (System.getProperty(propertyStr)).append(" ");
return buffer.toString();
}
private static String getSystemProperty() {
buffer = new StringBuffer();
initProperty("java.vendor.url","java.vendor.url");
initProperty("java.class.path","java.class.path");
return buffer.toString();
}
上述代碼主要是通過(guò)調(diào)用系統(tǒng)提供的System.getProperty方法獲取指定的系統(tǒng)信息,并合并成字符串返回。
1.2.2.3 運(yùn)營(yíng)商信息
運(yùn)營(yíng)商信息中包含IMEI、手機(jī)號(hào)碼等,在Android中提供了運(yùn)營(yíng)商管理類(TelephonyManager),可以通過(guò)TelephonyManager來(lái)獲取運(yùn)營(yíng)商相關(guān)的信息,實(shí)現(xiàn)的關(guān)鍵代碼如下:
Java代碼:
public static String fetch_tel_status(Context cx) {
String result = null;
TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
String str = " ";
str += "DeviceId(IMEI) = " + tm.getDeviceId() + " ";
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()+" ";
// TODO: Do something ...
int mcc = cx.getResources().getConfiguration().mcc;
int mnc = cx.getResources().getConfiguration().mnc;
str +="IMSI MCC (Mobile Country Code): " +String.valueOf(mcc) + " ";
str +="IMSI MNC (Mobile Network Code): " +String.valueOf(mnc) + " ";
result = str;
return result;
}在上述的代碼中,首先調(diào)用系統(tǒng)的getSystemService (Context.TELEPHONY_SERVICE)方法獲取一個(gè)TelephonyManager對(duì)象tm,進(jìn)而調(diào)用其方法 getDeviceId()獲取DeviceId信息,調(diào)用getDeviceSoftware Version()獲取設(shè)備的軟件版本信息等。
1.2.3 查看硬件信息
1.2.3.1 獲取CPU信息
可以在手機(jī)設(shè)備的/proc/cpuinfo中獲取CPU信息,調(diào)用CMDEexecute執(zhí)行系統(tǒng)的cat的命令,取/proc/cpuinfo的內(nèi)容,顯示的就是其CPU信息,實(shí)現(xiàn)代碼如下:
Java代碼:
在上述的代碼中,首先調(diào)用系統(tǒng)的getSystemService (Context.TELEPHONY_SERVICE)方法獲取一個(gè)TelephonyManager對(duì)象tm,進(jìn)而調(diào)用其方法 getDeviceId()獲取DeviceId信息,調(diào)用getDeviceSoftware Version()獲取設(shè)備的軟件版本信息等。
1.2.3 查看硬件信息
1.2.3.1 獲取CPU信息
可以在手機(jī)設(shè)備的/proc/cpuinfo中獲取CPU信息,調(diào)用CMDEexecute執(zhí)行系統(tǒng)的cat的命令,取/proc/cpuinfo的內(nèi)容,顯示的就是其CPU信息,實(shí)現(xiàn)代碼如下:
Java代碼:
public static String fetch_cpu_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[ ] args = {"/system/bin/cat", "/proc/cpuinfo"};
result = cmdexe.run(args, "/system/bin/");
Log.i("result", "result=" + result);
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}
上述代碼使用CMDExecute,調(diào)用系統(tǒng)中的"/system/bin/cat"命令查看"/proc/cpuinfo"中的內(nèi)容,即可得到CPU信息。
原文:
在開(kāi)發(fā)中 我們有時(shí)候會(huì)需要獲取當(dāng)前手機(jī)的系統(tǒng)版本來(lái)進(jìn)行判斷,或者需要獲取一些當(dāng)前手機(jī)的硬件信息。
android.os.Build類中。包括了這樣的一些信息。我們可以直接調(diào)用 而不需要添加任何的權(quán)限和方法。
android.os.Build.VERSION_CODES類 中有所有的已公布的Android版本號(hào)。全部是Int常亮。可用于與SDK_INT進(jìn)行比較來(lái)判斷當(dāng)前的系統(tǒng)版本
1.獲取系統(tǒng)的可用內(nèi)存和總內(nèi)存。
獲取系統(tǒng)內(nèi)存中應(yīng)用的信息,需要用到ActivityManager這個(gè)類,然而當(dāng)你用這個(gè)類拿數(shù)據(jù)的時(shí)候你會(huì)發(fā)現(xiàn),拿到的數(shù)據(jù)不正確。用這個(gè)類的API獲取系統(tǒng)的總內(nèi)存和可用內(nèi)存會(huì)出現(xiàn)數(shù)據(jù)不正確的情況。除了這個(gè)類,Android手機(jī)中有文件描述了這些信息——/proc/meminfo。meminfo文件中詳細(xì)的記錄了安卓手機(jī)的一些數(shù)據(jù),包括可用內(nèi)存和總內(nèi)存。附上代碼:
public static long getTotalMemSize() {
long size=0;
File file = new File("/proc/meminfo");
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String memInfo = buffer.readLine();
int startIndex = memInfo.indexOf(":");
int endIndex = memInfo.indexOf("k");
memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
size = Long.parseLong(memInfo);
size *= 1024;
buffer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return size;
}
public static long getAviableMemSize() {
long size=0;
File file = new File("/proc/meminfo");
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String memInfos=new String();
int i=0;
while ((memInfos=buffer.readLine())!=null){
i++;
if (i==2){
memInfo = memInfos;
}
}
int startIndex = memInfo.indexOf(":");
int endIndex = memInfo.indexOf("k");
memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
size = Long.parseLong(memInfo);
size *= 1024;
buffer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return size;
}
操作很簡(jiǎn)單分別是讀取第一行的數(shù)據(jù)和第二行的數(shù)據(jù),將字符串分去出,將所得值乘以1024變?yōu)閎yte類型。
2.獲取內(nèi)存中運(yùn)行應(yīng)用的信息
首先,自然要有一個(gè)Bean文件用于存儲(chǔ)這些信息,之后通過(guò)ActivityManager的getRunningAppProcesses()方法得到一個(gè)RunningAppProcessInfo的List。便利這個(gè)List去除我們想要的數(shù)據(jù),存在我們的Bean文件夾中。
public static ListTaskBean getAllTask() {
ListTaskBeantaskList=new ArrayList();
ListActivityManager.RunningAppProcessInforunList=UIUtils.getActManager().getRunningAppProcesses();
try {
for (ActivityManager.RunningAppProcessInfo r:runList) {
TaskBean taskBean = new TaskBean();
String processName = r.processName;
taskBean.setPackageName(processName);
PackageInfo packageInfo = UIUtils.getPacManager().getPackageInfo(processName, 0);
taskBean.setIcon(packageInfo.applicationInfo.loadIcon(UIUtils.getPacManager()));
taskBean.setName(packageInfo.applicationInfo.loadLabel(UIUtils.getPacManager()).toString());
Debug.MemoryInfo[] processInfo=UIUtils.getActManager().getProcessMemoryInfo(new int[]{r.pid});
taskBean.setMemSize(processInfo[0].getTotalPrivateDirty()*1024);
if ((packageInfo.applicationInfo.flagsApplicationInfo.FLAG_SYSTEM)!=0){
taskBean.setSystem(true);
}else {
taskBean.setUser(true);
}
if (taskList != null) {
taskList.add(taskBean);
for (int i=0;itaskList.size();i++) {
if (taskList.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){
taskList.remove(i);
}
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return taskList;
}
好了,大功告成。當(dāng)你開(kāi)開(kāi)心心的拿到手機(jī)上調(diào)試的時(shí)候你會(huì)發(fā)現(xiàn),一個(gè)數(shù)據(jù)都沒(méi)有。原來(lái),在Android5.0之后,谷歌處于完全考慮已經(jīng)棄用了通過(guò)如上方法拿到進(jìn)程中的信息。那么又應(yīng)該怎么做呢?
public static ListTaskBean getTaskInfos() {
ListAndroidAppProcess processInfos = ProcessManager.getRunningAppProcesses();
ListTaskBean taskinfos = new ArrayListTaskBean();
// 遍歷運(yùn)行的程序,并且獲取其中的信息
for (AndroidAppProcess processInfo : processInfos) {
TaskBean taskinfo = new TaskBean();
// 應(yīng)用程序的包名
String packname = processInfo.name;
taskinfo.setPackageName(packname);
// 湖區(qū)應(yīng)用程序的內(nèi)存 信息
android.os.Debug.MemoryInfo[] memoryInfos = UIUtils.getActManager()
.getProcessMemoryInfo(new int[] { processInfo.pid });
long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024L;
taskinfo.setMemSize(memsize);
taskinfo.setPackageName(processInfo.getPackageName());
try {
// 獲取應(yīng)用程序信息
ApplicationInfo applicationInfo = UIUtils.getPacManager().getApplicationInfo(
packname, 0);
Drawable icon = applicationInfo.loadIcon(UIUtils.getPacManager());
taskinfo.setIcon(icon);
String name = applicationInfo.loadLabel(UIUtils.getPacManager()).toString();
taskinfo.setName(name);
if ((applicationInfo.flags ApplicationInfo.FLAG_SYSTEM) == 0) {
// 用戶進(jìn)程
taskinfo.setUser(true);
} else {
// 系統(tǒng)進(jìn)程
taskinfo.setSystem(true);
}
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 系統(tǒng)內(nèi)核進(jìn)程 沒(méi)有名稱
taskinfo.setName(packname);
Drawable icon = UIUtils.getContext().getResources().getDrawable(
R.drawable.ic_launcher);
taskinfo.setIcon(icon);
}
if (taskinfo != null) {
taskinfos.add(taskinfo);
for (int i=0;itaskinfos.size();i++) {
if (taskinfos.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){
taskinfos.remove(i);
}
}
}
}
return taskinfos;
}
好了,接下來(lái)只需要判斷安裝的版本就可以了:
int sysVersion = Integer.parseInt(Build.VERSION.SDK);
taskList = sysVersion 21 ? TaskManagerEngine.getTaskInfos() : TaskManagerEngine.getAllTask();
好了,大功告成。數(shù)據(jù)就能正常拿到了。
1、CPU頻率,CPU信息:/proc/cpuinfo和/proc/stat
通過(guò)讀取文件/proc/cpuinfo系統(tǒng)CPU的類型等多種信息。
讀取/proc/stat 所有CPU活動(dòng)的信息來(lái)計(jì)算CPU使用率
下面我們就來(lái)講講如何通過(guò)代碼來(lái)獲取CPU頻率:
復(fù)制代碼 代碼如下:
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" 存儲(chǔ)最大頻率的文件的.路徑
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();
}
// 實(shí)時(shí)獲取CPU當(dāng)前頻率(單位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、內(nèi)存:/proc/meminfo
復(fù)制代碼 代碼如下:
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大小
復(fù)制代碼 代碼如下:
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大小
復(fù)制代碼 代碼如下:
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、電池電量
復(fù)制代碼 代碼如下:
private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
// level加%就是當(dāng)前電量了
}
};
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
6、系統(tǒng)的版本信息
復(fù)制代碼 代碼如下:
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地址和開(kāi)機(jī)時(shí)間
復(fù)制代碼 代碼如下:
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信息獲取,安卓app獲取手機(jī)信息
瀏覽路徑:http://chinadenli.net/article27/dsgdccj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、Google、面包屑導(dǎo)航、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容