Android开发技术 - - 系统资源监视_第1页
Android开发技术 - - 系统资源监视_第2页
Android开发技术 - - 系统资源监视_第3页
Android开发技术 - - 系统资源监视_第4页
Android开发技术 - - 系统资源监视_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

获取电量信息/**

*广播监听电量变化

*/

classBatteryReceiverextendsBroadcastReceiver{

//判断它是否是为电量变化的BroadcastAction

@Override

publicvoidonReceive(Contextcontext,Intentintent){

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

//获取当前电量

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

//电量的总刻度

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

//把它转成百分比

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

isBatteryOpen=false;

}

}

}获取电量信息/**

*注册广播得到系统电量的值

*/

privatevoidbatteryState(){

isBatteryOpen=true;

//注册receiver

registerReceiver(batteryReceiver,intentFilter);

}batteryReceiver=newBatteryReceiver();

//注册广播接受者监听系统电池变化

intentFilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);获取蓝牙信息/**

*判断蓝牙服务

*/

privateStringblueToothState(){

StringblueToothState="蓝牙已关闭";

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

booleanisBlueOpen=mBluetoothAdapter.isEnabled();

if(isBlueOpen){

blueToothState="蓝牙已打开";

}else{

blueToothState="蓝牙已关闭";

}

}

returnblueToothState;

}获取电话状态信息/**

*电话状态监听器

*/

privateclassMyPhoneStateListenerextendsPhoneStateListener{

/*从得到的信号强度,每个tiome供应商有更新*/

@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(),

"当前"

+level,

Toast.LENGTH_SHORT).show();

}

isGsmOpen=false;

}

}获取电话状态信息Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);MyListener=newMyPhoneStateListener();获取网络状态信息/**

*网络变化广播接收器

*/

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;

}

}

}获取网络状态信息/**

*注册网络变化广播

*/

privatevoidregisterReceiver(){

IntentFilterfilter=newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

myReceiver=newConnectionChangeReceiver();

this.registerReceiver(myReceiver,filter);

}

/**

*注销网络变化广播

*/

privatevoidunregisterReceiver(){

this.unregisterReceiver(myReceiver);

}

获取CPU使用信息/**

*获取系统总CPU使用时间

*/

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使用信息/**

*获取应用占用的CPU时间

*/

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;

}获取应用内存使用信息Android为每个应用分配多少内存?Android为每个进程分配内存时,采用弹性的分配方式,即刚开始并不会给应用分配很多的内存,而是给每一个进程分配一个“够用”的内存大小。分配最大内存AndroidManifest.xml中的application标签加上 android:largeHeap=“true“可以获取到最大分配内存NX510J手机实测配置之前,通过rt.maxMemory();获取的值为192M。设置largeHeap为true时,通过rt.maxMemory();获取的值为512M。获取应用内存使用信息/**

*获取应用内存使用信息

*/

privateStringgetMemory(){

ActivityManageractivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);

//最大分配内存

intmemory=activityManager.getMemoryClass();

//最大分配内存获取方法2

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

//当前分配的总内存

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

//剩余内存

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";

}获取系统内存使用信息/**

*获取系统内存使用信息

*/

privateStringgetMemoryInfo(){

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

ActivityManager.MemoryInfoinfo=newActivityManager.MemoryInfo();

manager.getMemoryInfo(info);

Log.e("Memory","系统总内存:"+(info.totalMem/(1024*1024))+"M");

Log.e("Memory","系统剩余内存:"+(info.availMem/(1024*1024))+"M");

Log.e("Memory","系统是否处于低内存运行:"+info.lowMemory);

Log.e("Memory","系统剩余内存低于"+(info.threshold/(1024*1024))+"M时为低内存运行");

return"系统剩余内存:"+(info.availMem/(1024*1024))+"M";

}获取存储使用信息android.os下的StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间,获取系统内部空间也就是system的大小和剩余空间。StatFs获取的都是以block为单位的获取存储使用信息block的概念:1.硬件上的blocksize,应该是“sectorsize”,linux的扇区大小是512byte

2.有文件系统的分区的blocksize,是“blocksize”,大小不一,可以用工具查看

3.没有文件系统的分区的blocksize,也叫“blocksize”,大小指的是1024byte

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

5.磁盘分区的“cylindersize”,用fdisk可以查看。一般SD卡都是fat32的文件系统,blocksize是4096.这样就可以知道手机的内部存储空间和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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论