Android系统管理工具类
2016-12-08 14:29
381 查看
这是一个系统管理工具类,管理sd卡,判断网络,uri转换,获取屏幕宽高,获取网络类型,隐藏软键盘,复制文本到粘贴板,获取状态栏高度,获取当前进程等。
上代码
import java.io.File;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ClipData;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
@SuppressWarnings("deprecation")
public class SystemUtil {
public static final int NETTYPE_WIFI = 0x01;
public static final int NETTYPE_CMWAP = 0x02;
public static final int NETTYPE_CMNET = 0x03;
/** 判断是否手机插入Sd卡 */
public static boolean sdCardUseable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取Sd卡的总容量
*
* @return
*/
@SuppressLint("NewApi") public static long getSdCardTotalSize() {
if(!sdCardUseable()){
return 0;
}
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSizeLong();
// 获取所有数据块数
long allBlocks = sf.getBlockCountLong();
// 返回SD卡大小
// return allBlocks * blockSize; //单位Byte
// return (allBlocks * blockSize)/1024; //单位KB
return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
}
/**
* 获取Sd卡的可用容量
*
* @return
*/
@SuppressLint("NewApi") public static long getSdCardFreeSize() {
if(!sdCardUseable()){
return 0;
}
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSizeLong();
// 空闲的数据块的数量
long freeBlocks = sf.getAvailableBlocksLong();
// 返回SD卡空闲大小
// return freeBlocks * blockSize; //单位Byte
// return (freeBlocks * blockSize)/1024; //单位KB
return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
}
/**
* 判断是否联网或者漫游
*
* @param context
* @return boolean
*/
public static boolean hasNet(Context context) {
// 获得ConnectivityManager的管理器
NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) { // 漫游判断
return true;
}
return true;
}
/** 获得The data stream for the file */
public static String getUrlPath(Activity context, Uri uri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/** 从传入Uri获得真实的path */
public String getRealPathFromURI(Activity context, Uri contentUri) {
// can post image
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.managedQuery(contentUri, proj, // Which columns
// to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/** 获得屏幕的宽度 */
public static int getScreenWidth(Activity context) {
DisplayMetrics outMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/** 获取屏幕的高度 */
public static int getScreenHeight(Activity context) {
DisplayMetrics outMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/** 获得网络的类型 */
public static int getNetworkType(Context context) {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) { // 判断是否联网
return netType;
}
int nType = networkInfo.getType(); // 获得
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!TextUtils.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
/** 隐藏软件盘 */
public static void hideSoftKeyborad(Activity context) {
final View v = context.getWindow().peekDecorView(); // Retrieve the
// current decor
// view
if (v != null && v.getWindowToken() != null) {
InputMethodManager imm = (InputMethodManager) context // 获得输入方法的Manager
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
/**
* 复制文本到剪切板
*
* @param context
* @param text
*/
@TargetApi(value = 11)
@SuppressLint({ "NewApi", "NewApi" })
public static void copyText(Context context, String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("label", text);
clipboardManager.setPrimaryClip(clipData);
} else {
android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(text);
}
}
/**
* 获取状态栏高度
*
* @return
*/
public static int getStatusBarHeight(Context context) {
Class<?> c = null;
Object obj = null;
java.lang.reflect.Field field = null;
int x = 0;
int statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
return statusBarHeight;
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
/**
* 获取当前进程名
* @param context
* @return 进程名
*/
public static final String getProcessName(Context context) {
String processName = null;
// ActivityManager
ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
while (true) {
for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
if (info.pid == android.os.Process.myPid()) {
processName = info.processName;
break;
}
}
// go home
if (!TextUtils.isEmpty(processName)) {
return processName;
}
// take a rest and again
try {
Thread.sleep(100L);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
上代码
import java.io.File;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ClipData;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
@SuppressWarnings("deprecation")
public class SystemUtil {
public static final int NETTYPE_WIFI = 0x01;
public static final int NETTYPE_CMWAP = 0x02;
public static final int NETTYPE_CMNET = 0x03;
/** 判断是否手机插入Sd卡 */
public static boolean sdCardUseable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取Sd卡的总容量
*
* @return
*/
@SuppressLint("NewApi") public static long getSdCardTotalSize() {
if(!sdCardUseable()){
return 0;
}
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSizeLong();
// 获取所有数据块数
long allBlocks = sf.getBlockCountLong();
// 返回SD卡大小
// return allBlocks * blockSize; //单位Byte
// return (allBlocks * blockSize)/1024; //单位KB
return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
}
/**
* 获取Sd卡的可用容量
*
* @return
*/
@SuppressLint("NewApi") public static long getSdCardFreeSize() {
if(!sdCardUseable()){
return 0;
}
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSizeLong();
// 空闲的数据块的数量
long freeBlocks = sf.getAvailableBlocksLong();
// 返回SD卡空闲大小
// return freeBlocks * blockSize; //单位Byte
// return (freeBlocks * blockSize)/1024; //单位KB
return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
}
/**
* 判断是否联网或者漫游
*
* @param context
* @return boolean
*/
public static boolean hasNet(Context context) {
// 获得ConnectivityManager的管理器
NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) { // 漫游判断
return true;
}
return true;
}
/** 获得The data stream for the file */
public static String getUrlPath(Activity context, Uri uri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/** 从传入Uri获得真实的path */
public String getRealPathFromURI(Activity context, Uri contentUri) {
// can post image
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.managedQuery(contentUri, proj, // Which columns
// to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/** 获得屏幕的宽度 */
public static int getScreenWidth(Activity context) {
DisplayMetrics outMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/** 获取屏幕的高度 */
public static int getScreenHeight(Activity context) {
DisplayMetrics outMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/** 获得网络的类型 */
public static int getNetworkType(Context context) {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) { // 判断是否联网
return netType;
}
int nType = networkInfo.getType(); // 获得
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!TextUtils.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
/** 隐藏软件盘 */
public static void hideSoftKeyborad(Activity context) {
final View v = context.getWindow().peekDecorView(); // Retrieve the
// current decor
// view
if (v != null && v.getWindowToken() != null) {
InputMethodManager imm = (InputMethodManager) context // 获得输入方法的Manager
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
/**
* 复制文本到剪切板
*
* @param context
* @param text
*/
@TargetApi(value = 11)
@SuppressLint({ "NewApi", "NewApi" })
public static void copyText(Context context, String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("label", text);
clipboardManager.setPrimaryClip(clipData);
} else {
android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(text);
}
}
/**
* 获取状态栏高度
*
* @return
*/
public static int getStatusBarHeight(Context context) {
Class<?> c = null;
Object obj = null;
java.lang.reflect.Field field = null;
int x = 0;
int statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
return statusBarHeight;
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
/**
* 获取当前进程名
* @param context
* @return 进程名
*/
public static final String getProcessName(Context context) {
String processName = null;
// ActivityManager
ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
while (true) {
for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
if (info.pid == android.os.Process.myPid()) {
processName = info.processName;
break;
}
}
// go home
if (!TextUtils.isEmpty(processName)) {
return processName;
}
// take a rest and again
try {
Thread.sleep(100L);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
相关文章推荐
- android开发 系统信息相关工具类 权限管理页面
- Android开发之超实用的系统管理工具类【SD卡,网络,uri,屏幕,网络,软键盘,文本,进程等】
- android view系统中无效区管理的bug
- android aapt管理系统资源frameworks-res
- Android内核与标准Linux内核在文件系统、进程间通信机制、内存管理等方面存在不同
- android的窗口机制分析------UI管理系统
- 第十三篇 Android 系统电话管理机制二
- Android数据存储--SQLite 存储方式应用(日记管理系统)
- Android系统Surface机制的SurfaceFlinger服务对帧缓冲区(Frame Buffer)的管理分析
- SQlite Android数据库的应用 Android SQLite 简易的学生成绩管理系统
- 如何让Android中的 Base64工具类支持2.2以下的系统
- Android系统Surface机制的SurfaceFlinger服务对帧缓冲区(Frame Buffer)的管理分析
- Android文件、内存、SDCard管理常用工具类、方法
- 学生管理系统-基于SQLite数据库的Android应用
- 适易信息管理系统架构、配置实现增删改查中工具类介绍
- Android系统的超级终端命令大全(四)--系统管理命令
- android中跳转到系统管理应用程序界面的方法
- android的窗口机制分析------UI管理系统
- 第十三篇 Android 系统电话管理机制与架构二