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

package com.nancal.pdaclient.util;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

public class PhoneStateUtil {

private static final String ERROR = "找不到存储设备";
//获取wifi信号强度
public String getWifiStatus(Context context) {
String result = null;
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wm.getConnectionInfo();
int rssi = connectionInfo.getRssi();
System.out.println("signal" + rssi);
if (rssi > -50) {
result = "wifi信号良好";
} else if (rssi <= -50 && rssi > 70) {
result = "wifi信号较差";
} else if (rssi <= -70) {
result = "wifi信号极差,有可能连接不上";
}
return result;
}
//获取电池电量
public String getBatteryStatus(Context context) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
return ((float) level / (float) scale) * 100.0f + "%";
}

//获取3G网网速
public String get3Gstatus(Context context) {
String result = null;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
int dataState = telephonyManager.getDataState();
if (dataState != 2) {
result = "移动网络已经关闭,请打开网络";
return result;
}
int networkType = telephonyManager.getNetworkType();

switch (networkType) {
case 1:
result = "3g网速极差,小于128K";
break;
case 2:
result = "3g网速较差,在128K 到 236k 之间";
break;
case 3:
result = "3g网速良好,在236k 到 2M之间";
break;
default:
break;
}
return result;
}
//是否有外部存储设备
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
//获取内部存储设备可用容量
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
//获取内部存储设备总容量
public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}
//获取外部存储可用的容量
public static String getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return ERROR;
}
}
//获取外部存储总容量
public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return ERROR;
}
}
//把byte单位转换成M
public static String formatSize(long size) {
String suffix = null;

if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}

StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}

if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
}

}

你可能感兴趣的:(手机常用方法, 获取电池电量,信号强度,sd存储容量等)