手机常用方法, 获取电池电量,信号强度,sd存储容量等

89751292 2013-10-23

packagecom.nancal.pdaclient.util;

importjava.io.File;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.content.IntentFilter;

importandroid.net.wifi.WifiInfo;

importandroid.net.wifi.WifiManager;

importandroid.os.BatteryManager;

importandroid.os.Environment;

importandroid.os.StatFs;

importandroid.telephony.TelephonyManager;

publicclassPhoneStateUtil{

privatestaticfinalStringERROR="找不到存储设备";

//获取wifi信号强度

publicStringgetWifiStatus(Contextcontext){

Stringresult=null;

WifiManagerwm=(WifiManager)context

.getSystemService(Context.WIFI_SERVICE);

WifiInfoconnectionInfo=wm.getConnectionInfo();

intrssi=connectionInfo.getRssi();

System.out.println("signal"+rssi);

if(rssi>-50){

result="wifi信号良好";

}elseif(rssi<=-50&&rssi>70){

result="wifi信号较差";

}elseif(rssi<=-70){

result="wifi信号极差,有可能连接不上";

}

returnresult;

}

//获取电池电量

publicStringgetBatteryStatus(Contextcontext){

IntentFilterifilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);

IntentbatteryStatus=context.registerReceiver(null,ifilter);

intlevel=batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);

intscale=batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE,-1);

return((float)level/(float)scale)*100.0f+"%";

}

//获取3G网网速

publicStringget3Gstatus(Contextcontext){

Stringresult=null;

TelephonyManagertelephonyManager=(TelephonyManager)context

.getSystemService(Context.TELEPHONY_SERVICE);

intdataState=telephonyManager.getDataState();

if(dataState!=2){

result="移动网络已经关闭,请打开网络";

returnresult;

}

intnetworkType=telephonyManager.getNetworkType();

switch(networkType){

case1:

result="3g网速极差,小于128K";

break;

case2:

result="3g网速较差,在128K到236k之间";

break;

case3:

result="3g网速良好,在236k到2M之间";

break;

default:

break;

}

returnresult;

}

//是否有外部存储设备

publicstaticbooleanexternalMemoryAvailable(){

returnandroid.os.Environment.getExternalStorageState().equals(

android.os.Environment.MEDIA_MOUNTED);

}

//获取内部存储设备可用容量

publicstaticStringgetAvailableInternalMemorySize(){

Filepath=Environment.getDataDirectory();

StatFsstat=newStatFs(path.getPath());

longblockSize=stat.getBlockSize();

longavailableBlocks=stat.getAvailableBlocks();

returnformatSize(availableBlocks*blockSize);

}

//获取内部存储设备总容量

publicstaticStringgetTotalInternalMemorySize(){

Filepath=Environment.getDataDirectory();

StatFsstat=newStatFs(path.getPath());

longblockSize=stat.getBlockSize();

longtotalBlocks=stat.getBlockCount();

returnformatSize(totalBlocks*blockSize);

}

//获取外部存储可用的容量

publicstaticStringgetAvailableExternalMemorySize(){

if(externalMemoryAvailable()){

Filepath=Environment.getExternalStorageDirectory();

StatFsstat=newStatFs(path.getPath());

longblockSize=stat.getBlockSize();

longavailableBlocks=stat.getAvailableBlocks();

returnformatSize(availableBlocks*blockSize);

}else{

returnERROR;

}

}

//获取外部存储总容量

publicstaticStringgetTotalExternalMemorySize(){

if(externalMemoryAvailable()){

Filepath=Environment.getExternalStorageDirectory();

StatFsstat=newStatFs(path.getPath());

longblockSize=stat.getBlockSize();

longtotalBlocks=stat.getBlockCount();

returnformatSize(totalBlocks*blockSize);

}else{

returnERROR;

}

}

//把byte单位转换成M

publicstaticStringformatSize(longsize){

Stringsuffix=null;

if(size>=1024){

suffix="KB";

size/=1024;

if(size>=1024){

suffix="MB";

size/=1024;

}

}

StringBuilderresultBuffer=newStringBuilder(Long.toString(size));

intcommaOffset=resultBuffer.length()-3;

while(commaOffset>0){

resultBuffer.insert(commaOffset,',');

commaOffset-=3;

}

if(suffix!=null)

resultBuffer.append(suffix);

returnresultBuffer.toString();

}

}