




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
獲取電量信息/**
*廣播監(jiān)聽(tīng)電量變化
*/
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;
}
}
}獲取電量信息/**
*注冊(cè)廣播得到系統(tǒng)電量的值
*/
privatevoidbatteryState(){
isBatteryOpen=true;
//注冊(cè)receiver
registerReceiver(batteryReceiver,intentFilter);
}batteryReceiver=newBatteryReceiver();
//注冊(cè)廣播接受者監(jiān)聽(tīng)系統(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)牙已打開(kāi)";
}else{
blueToothState="藍(lán)牙已關(guān)閉";
}
}
returnblueToothState;
}獲取電話狀態(tài)信息/**
*電話狀態(tài)監(jiān)聽(tīng)器
*/
privateclassMyPhoneStateListenerextendsPhoneStateListener{
/*從得到的信號(hào)強(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)信息/**
*注冊(cè)網(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í),采用彈性的分配方式,即剛開(kāi)始并不會(huì)給應(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í)測(cè)配置之前,通過(guò)rt.maxMemory();獲取的值為192M。設(shè)置largeHeap為true時(shí),通過(guò)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";
}獲取存儲(chǔ)使用信息android.os下的StatFs類主要用來(lái)獲取文件系統(tǒng)的狀態(tài),能夠獲取sd卡的大小和剩余空間,獲取系統(tǒng)內(nèi)部空間也就是system的大小和剩余空間。StatFs獲取的都是以block為單位的獲取存儲(chǔ)使用信息block的概念:1.硬件上的blocksize,應(yīng)該是“sectorsize”,linux的扇區(qū)大小是512byte
2.有文件系統(tǒng)的分區(qū)的blocksize,是“blocksize”,大小不一,可以用工具查看
3.沒(méi)有文件系統(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)部存儲(chǔ)空間和sd卡存儲(chǔ)空間的總大小和可用大小了獲取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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公路客運(yùn)企業(yè)發(fā)展戰(zhàn)略考核試卷
- 玉石真假鑒別與防偽技術(shù)考核試卷
- 橡膠環(huán)保設(shè)備開(kāi)發(fā)考核試卷
- 理清思路的2024年SCMP考試要點(diǎn)與試題及答案
- 細(xì)胞內(nèi)代謝途徑類型及其聯(lián)系試題及答案
- 全媒體運(yùn)營(yíng)師內(nèi)容創(chuàng)作的趨勢(shì)與挑戰(zhàn):試題及答案
- 馬工學(xué)管理中的創(chuàng)新應(yīng)用案例分析試題及答案
- 硬骨魚(yú)與軟骨魚(yú)比較試題及答案
- 2025年LED室內(nèi)應(yīng)用燈具項(xiàng)目發(fā)展計(jì)劃
- 年度經(jīng)營(yíng)工作發(fā)言稿
- HJ 636-2012 水質(zhì) 總氮的測(cè)定 堿性過(guò)硫酸鉀消解紫外分光光度法
- 《機(jī)場(chǎng)運(yùn)行管理》考試復(fù)習(xí)題庫(kù)(含答案)
- JBT 7248-2024 閥門用低溫鋼鑄件技術(shù)規(guī)范(正式版)
- 2024專升本英語(yǔ)答題卡浙江省
- 2024年荊門市水務(wù)局事業(yè)單位公開(kāi)招聘工作人員招聘歷年公開(kāi)引進(jìn)高層次人才和急需緊缺人才筆試參考題庫(kù)(共500題)答案詳解版
- 反食品浪費(fèi)及食品安全與健康
- 【勞動(dòng)教育一等獎(jiǎng)教學(xué)案例】-二年級(jí)勞動(dòng)教育-《三股辮兒我會(huì)編》活動(dòng)方案
- 校園模擬法庭劇本
- 水準(zhǔn)測(cè)量記錄數(shù)據(jù)自動(dòng)生成表
- 肝門部膽管癌護(hù)理查房課件
- 公司經(jīng)營(yíng)合同-公司代持股份協(xié)議范本新
評(píng)論
0/150
提交評(píng)論