Android開發(fā)技術(shù) - - 系統(tǒng)資源監(jiān)視_第1頁
Android開發(fā)技術(shù) - - 系統(tǒng)資源監(jiān)視_第2頁
Android開發(fā)技術(shù) - - 系統(tǒng)資源監(jiān)視_第3頁
Android開發(fā)技術(shù) - - 系統(tǒng)資源監(jiān)視_第4頁
Android開發(fā)技術(shù) - - 系統(tǒng)資源監(jiān)視_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

獲取電量信息/**

*廣播監(jiān)聽電量變化

*/

classBatteryReceiverextendsBroadcastReceiver{

//判斷它是否是為電量變化的BroadcastAction

@Override

publicvoidonReceive(Contextcontext,Intentintent){

if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())&&isBatteryOpen){

//獲取當(dāng)前電量

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

//電量的總刻度

intscale=intent.getIntExtra("scale",100);

//把它轉(zhuǎn)成百分比

Toast.makeText(context,"電池電量為"+((level*100)/scale)+"%",Toast.LENGTH_LONG).show();

isBatteryOpen=false;

}

}

}獲取電量信息/**

*注冊廣播得到系統(tǒng)電量的值

*/

privatevoidbatteryState(){

isBatteryOpen=true;

//注冊receiver

registerReceiver(batteryReceiver,intentFilter);

}batteryReceiver=newBatteryReceiver();

//注冊廣播接受者監(jiān)聽系統(tǒng)電池變化

intentFilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);獲取藍(lán)牙信息/**

*判斷藍(lán)牙服務(wù)

*/

privateStringblueToothState(){

StringblueToothState="藍(lán)牙已關(guān)閉";

if(mBluetoothAdapter!=null){ //BluetoothAdapter.getDefaultAdapter();

booleanisBlueOpen=mBluetoothAdapter.isEnabled();

if(isBlueOpen){

blueToothState="藍(lán)牙已打開";

}else{

blueToothState="藍(lán)牙已關(guān)閉";

}

}

returnblueToothState;

}獲取電話狀態(tài)信息/**

*電話狀態(tài)監(jiān)聽器

*/

privateclassMyPhoneStateListenerextendsPhoneStateListener{

/*從得到的信號強(qiáng)度,每個(gè)tiome供應(yīng)商有更新*/

@Override

publicvoidonSignalStrengthsChanged(SignalStrengthsignalStrength){

super.onSignalStrengthsChanged(signalStrength);

Stringlevel;

if(isGsmOpen){

intasu=signalStrength.getGsmSignalStrength();

if(asu<=2||asu==99)level=SIGNAL_STRENGTH_NONE_OR_UNKNOWN;

elseif(asu>=12)level=SIGNAL_STRENGTH_GREAT;

elseif(asu>=8)level=SIGNAL_STRENGTH_GOOD;

elseif(asu>=5)level=SIGNAL_STRENGTH_MODERATE;

elselevel=SIGNAL_STRENGTH_POOR;

Toast.makeText(getApplicationContext(),

"當(dāng)前"

+level,

Toast.LENGTH_SHORT).show();

}

isGsmOpen=false;

}

}獲取電話狀態(tài)信息Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);MyListener=newMyPhoneStateListener();獲取網(wǎng)絡(luò)狀態(tài)信息/**

*網(wǎng)絡(luò)變化廣播接收器

*/

publicclassConnectionChangeReceiverextendsBroadcastReceiver{

@Override

publicvoidonReceive(Contextcontext,Intentintent){

ConnectivityManagerconnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfomobNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

NetworkInfowifiNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if(!mobNetInfo.isConnected()&&!wifiNetInfo.isConnected()){

isConnect=false;

}else{

isConnect=true;

}

}

}獲取網(wǎng)絡(luò)狀態(tài)信息/**

*注冊網(wǎng)絡(luò)變化廣播

*/

privatevoidregisterReceiver(){

IntentFilterfilter=newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

myReceiver=newConnectionChangeReceiver();

this.registerReceiver(myReceiver,filter);

}

/**

*注銷網(wǎng)絡(luò)變化廣播

*/

privatevoidunregisterReceiver(){

this.unregisterReceiver(myReceiver);

}

獲取CPU使用信息/**

*獲取系統(tǒng)總CPU使用時(shí)間

*/

publicstaticlonggetTotalCpuTime(){

String[]cpuInfos=null;

try{

BufferedReaderreader=newBufferedReader(newInputStreamReader(

newFileInputStream("/proc/stat")),1000);

Stringload=reader.readLine();

Log.i(TAG,"getTotalCpuTime:"+load);

reader.close();

cpuInfos=load.split("");

}catch(IOExceptionex){

ex.printStackTrace();

}

longtotalCpu=Long.parseLong(cpuInfos[2])

+Long.parseLong(cpuInfos[3])+Long.parseLong(cpuInfos[4])

+Long.parseLong(cpuInfos[6])+Long.parseLong(cpuInfos[5])

+Long.parseLong(cpuInfos[7])+Long.parseLong(cpuInfos[8]);

returntotalCpu;

}獲取CPU使用信息/**

*獲取應(yīng)用占用的CPU時(shí)間

*/

publicstaticlonggetAppCpuTime(){

String[]cpuInfos=null;

try{

intpid=android.os.Process.myPid();

BufferedReaderreader=newBufferedReader(newInputStreamReader(

newFileInputStream("/proc/"+pid+"/stat")),1000);

Stringload=reader.readLine();

Log.i(TAG,"getAppCpuTime:"+load);

reader.close();

cpuInfos=load.split("");

}catch(IOExceptionex){

ex.printStackTrace();

}

longappCpuTime=Long.parseLong(cpuInfos[13])

+Long.parseLong(cpuInfos[14])+Long.parseLong(cpuInfos[15])

+Long.parseLong(cpuInfos[16]);

returnappCpuTime;

}獲取CPU使用信息/**

*獲取CPU使用率

*/

privatedoublegetProcessCpuRate(){

floattotalCpuTime1=getTotalCpuTime();

floatprocessCpuTime1=getAppCpuTime();

try{

Thread.sleep(360);

}catch(Exceptione){

e.printStackTrace();

}

floattotalCpuTime2=getTotalCpuTime();

floatprocessCpuTime2=getAppCpuTime();

floatcpuRate=100*(processCpuTime2-processCpuTime1)

/(totalCpuTime2-totalCpuTime1);

returncpuRate;

}獲取應(yīng)用內(nèi)存使用信息Android為每個(gè)應(yīng)用分配多少內(nèi)存?Android為每個(gè)進(jìn)程分配內(nèi)存時(shí),采用彈性的分配方式,即剛開始并不會給應(yīng)用分配很多的內(nèi)存,而是給每一個(gè)進(jìn)程分配一個(gè)“夠用”的內(nèi)存大小。分配最大內(nèi)存AndroidManifest.xml中的application標(biāo)簽加上 android:largeHeap=“true“可以獲取到最大分配內(nèi)存NX510J手機(jī)實(shí)測配置之前,通過rt.maxMemory();獲取的值為192M。設(shè)置largeHeap為true時(shí),通過rt.maxMemory();獲取的值為512M。獲取應(yīng)用內(nèi)存使用信息/**

*獲取應(yīng)用內(nèi)存使用信息

*/

privateStringgetMemory(){

ActivityManageractivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);

//最大分配內(nèi)存

intmemory=activityManager.getMemoryClass();

//最大分配內(nèi)存獲取方法2

floatmaxMemory=(float)(Runtime.getRuntime().maxMemory()*1.0/(1024*1024));

//當(dāng)前分配的總內(nèi)存

floattotalMemory=(float)(Runtime.getRuntime().totalMemory()*1.0/(1024*1024));

//剩余內(nèi)存

floatfreeMemory=(float)(Runtime.getRuntime().freeMemory()*1.0/(1024*1024));

System.out.println("maxMemory:"+maxMemory);

System.out.println("totalMemory:"+totalMemory);

System.out.println("freeMemory:"+freeMemory);

return"maxMemory:"+maxMemory+"M;totalMemory:"+totalMemory+"M;freeMemory:"+freeMemory+"M";

}獲取系統(tǒng)內(nèi)存使用信息/**

*獲取系統(tǒng)內(nèi)存使用信息

*/

privateStringgetMemoryInfo(){

ActivityManagermanager=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

ActivityManager.MemoryInfoinfo=newActivityManager.MemoryInfo();

manager.getMemoryInfo(info);

Log.e("Memory","系統(tǒng)總內(nèi)存:"+(info.totalMem/(1024*1024))+"M");

Log.e("Memory","系統(tǒng)剩余內(nèi)存:"+(info.availMem/(1024*1024))+"M");

Log.e("Memory","系統(tǒng)是否處于低內(nèi)存運(yùn)行:"+info.lowMemory);

Log.e("Memory","系統(tǒng)剩余內(nèi)存低于"+(info.threshold/(1024*1024))+"M時(shí)為低內(nèi)存運(yùn)行");

return"系統(tǒng)剩余內(nèi)存:"+(info.availMem/(1024*1024))+"M";

}獲取存儲使用信息android.os下的StatFs類主要用來獲取文件系統(tǒng)的狀態(tài),能夠獲取sd卡的大小和剩余空間,獲取系統(tǒng)內(nèi)部空間也就是system的大小和剩余空間。StatFs獲取的都是以block為單位的獲取存儲使用信息block的概念:1.硬件上的blocksize,應(yīng)該是“sectorsize”,linux的扇區(qū)大小是512byte

2.有文件系統(tǒng)的分區(qū)的blocksize,是“blocksize”,大小不一,可以用工具查看

3.沒有文件系統(tǒng)的分區(qū)的blocksize,也叫“blocksize”,大小指的是1024byte

4.Kernelbuffercache的blocksize,就是“blocksize”,大部分PC是1024byte

5.磁盤分區(qū)的“cylindersize”,用fdisk可以查看。一般SD卡都是fat32的文件系統(tǒng),blocksize是4096.這樣就可以知道手機(jī)的內(nèi)部存儲空間和sd卡存儲空間的總大小和可用大小了獲取SDCard使用信息/**

*獲取SDCard使用信息

*/

StringreadSDCard(){

Stringstate=Environment.getExternalStorageState();

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

FilesdcardDir=Environment.getExternalStorageDirectory();

StatFssf=newStatFs(sdcardDir.getPath());

longblockSize=sf.getBlockSize();

longblockCount=sf.getBlockCount();

longavail

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論