Android Util工具類的封裝
阿新 • • 發佈:2019-02-09
摘要
本文總結了過往專案和一些資料上的Android 工具類的封裝,既當做是總結,也作收集收藏用。感謝網路上諸方資料。
SharedPreferences工具類:SPUtil.java
public class SPUtil { public static final String FILE_DEFAULT = "default"; public static int getInt(String key) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getInt(key, 0); } public static boolean getBoolean(String key) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getBoolean(key, false); } public static float getFloat(String key) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getFloat(key, 0); } public static boolean getBoolean(String key, boolean defValue) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getBoolean(key, defValue); } public static String getString(String key) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getString(key,FILE_DEFAULT); } public static String getStringWithDefault(String key, String defStr) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getString(key, defStr); } public static void putInt(String key, int value) { SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT)); editor.putInt(key, value); editor.commit(); } public static void putBoolean(String key, boolean value) { SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT)); editor.putBoolean(key, value); editor.commit(); } public static void putString(String key, String value) { SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT)); editor.putString(key, value); editor.commit(); } public static void putFloat(String key, float value) { SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT)); editor.putFloat(key, value); editor.commit(); } public static void putLong(String key, long value) { SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT)); editor.putLong(key, value); editor.commit(); } public static long getLong(String key) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getLong(key, 0); } public static int getInt(String key, int defValue) { SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT); return sharedPreferences.getInt(key, defValue); } public static int getInt(String key, String fileName) { SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName)); return sharedPreferences.getInt(key, 0); } public static boolean getBoolean(String key, String fileName) { SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName)); return sharedPreferences.getBoolean(key, false); } public static String getString(String key, String fileName) { SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName)); return sharedPreferences.getString(key, null); } public static float getFloat(String key, String fileName) { SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName)); return sharedPreferences.getFloat(key, 0); } public static long getLong(String key, String fileName) { SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName)); return sharedPreferences.getLong(key, 0); } public static void putInt(String key, int value, String fileName) { SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName))); editor.putInt(key, value); editor.commit(); } public static void putBoolean(String key, boolean value, String fileName) { SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName))); editor.putBoolean(key, value); editor.commit(); } public static void putString(String key, String value, String fileName) { SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName))); editor.putString(key, value); editor.commit(); } public static void putFloat(String key, float value, String fileName) { SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName))); editor.putFloat(key, value); editor.commit(); } public static void putLong(String key, long value, String fileName) { SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName))); editor.putLong(key, value); editor.commit(); } private static SharedPreferences getSharePreferences(String fileName) { return TApplication.getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE); } private static SharedPreferences.Editor getEditor(SharedPreferences sharedPreferences) { return sharedPreferences.edit(); } public static String getFileName(String fileName) { if (!TextUtils.isEmpty(fileName)) { return fileName; } else { return FILE_DEFAULT; } } }
ToastUtil.java
public class ToastUtils {
private static Toast mToast ;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
@Override
public void run() {
mToast.cancel();
}
} ;
/**
* 自定義toast,重複點選時,會直接替換文字,不會重新建立新的Toast並加入佇列中一條條顯示
* @param context
* @param text
* @param duration
*/
public static void showToast(Context context,String text,int duration) {
mHandler.removeCallbacks(r);
if (mToast != null) {
mToast.setText(text);
} else {
mToast = Toast.makeText(context,text,Toast.LENGTH_SHORT);
}
mHandler.postDelayed(r,duration) ;
mToast.show();
}
/**
* 防重複點選的toast,即便是帕金森患者也不會導致toast一直顯示著
* @param text
*/
public static void showToast(String text) {
showToast(TApplication.getContext(),text,1000);
}
public static void showToast(Context context,int resId,int duration) {
showToast(context,context.getResources().getString(resId),duration);
}
public static void show(CharSequence text) {
Toast.makeText(TApplication.getContext(), text, Toast.LENGTH_SHORT).show();
}
/**
* 顯示土司的工具類,安全的工具類可以在任意執行緒裡面顯示
* @param activity
* @param text
*/
public static void show(final Activity activity, final CharSequence text) {
if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
if (activity == null){
return;
}
Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
} else {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
}
});
}
}
/**
* 顯示土司的工具類,安全的工具類可以在任意執行緒裡面顯示
* @param activity
* @param text
* @param length 顯示的時常
*/
public static void show(final Activity activity, final CharSequence text, final int length) {
if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
Toast.makeText(activity, text, length).show();
} else {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, text, length).show();
}
});
}
}
}
DensityUtils.java
public class DensityUtils {
private DensityUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* dp轉px
*
* @param context
* @param val
* @return
*/
public static int dp2px(Context context, float dpVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp轉px
*
* @param context
* @param val
* @return
*/
public static int sp2px(Context context, float spVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
/**
* px轉dp
*
* @param context
* @param pxVal
* @return
*/
public static float px2dp(Context context, float pxVal)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px轉sp
*
* @param fontScale
* @param pxVal
* @return
*/
public static float px2sp(Context context, float pxVal)
{
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
SDCardUtils.java
public class SDCardUtils {
private SDCardUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判斷SDCard是否可用
*
* @return
*/
public static boolean isSDCardEnable()
{
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
* 獲取SD卡路徑
*
* @return
*/
public static String getSDCardPath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
/**
* 獲取SD卡的剩餘容量 單位byte
*
* @return
*/
public static long getSDCardAllSize()
{
if (isSDCardEnable())
{
StatFs stat = new StatFs(getSDCardPath());
// 獲取空閒的資料塊的數量
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// 獲取單個數據塊的大小(byte)
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}
/**
* 獲取指定路徑所在空間的剩餘可用容量位元組數,單位byte
*
* @param filePath
* @return 容量位元組 SDCard可用空間,內部儲存可用空間
*/
public static long getFreeBytes(String filePath)
{
// 如果是sd卡的下的路徑,則獲取sd卡可用容量
if (filePath.startsWith(getSDCardPath()))
{
filePath = getSDCardPath();
} else
{// 如果是內部儲存的路徑,則獲取記憶體儲存的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
/**
* 獲取系統儲存路徑
*
* @return
*/
public static String getRootDirectoryPath()
{
return Environment.getRootDirectory().getAbsolutePath();
}
}
螢幕輔助類ScreenUtils.java
public class ScreenUtils {
private ScreenUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 獲得螢幕高度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 獲得螢幕寬度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 獲得狀態列的高度
*
* @param context
* @return
*/
public static int getStatusHeight(Context context)
{
int statusHeight = -1;
try
{
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e)
{
e.printStackTrace();
}
return statusHeight;
}
/**
* 獲取當前螢幕截圖,包含狀態列
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 獲取當前螢幕截圖,不包含狀態列
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
AppUtils
public class AppUtils {
private AppUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 獲取應用程式名稱
*/
public static String getAppName(Context context)
{
try
{
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (NameNotFoundException e)
{
e.printStackTrace();
}
return null;
}
/**
* [獲取應用程式版本名稱資訊]
*
* @param context
* @return 當前應用的版本名稱
*/
public static String getVersionName(Context context)
{
try
{
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (NameNotFoundException e)
{
e.printStackTrace();
}
return null;
}
}
NetUtils.java
public class NetUtils {
public static final int NO_NETWORK = 0;
public static final int NETWORK_WIFI = 1;
public static final int NETWORK_MOBILE = 2;
private NetUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判斷網路是否連線
*/
public static boolean isConnected(Context context)
{
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity)
{
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected())
{
if (info.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
/**
* 判斷是否是wifi連線
*/
public static boolean isWifi(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 開啟網路設定介面
*/
public static void openSetting(Activity activity)
{
Intent intent = new Intent("/");
ComponentName cm = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(cm);
intent.setAction("android.intent.action.VIEW");
activity.startActivityForResult(intent, 0);
}
/**
* 檢查網路連線
*
* @param context
* @return 0為無網路狀態,1為wifi連線狀態,2為行動網路連線狀態
*/
public static int checkNetwork(Context context) {
if (context == null) {
return NETWORK_WIFI;
}
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi != null && wifi.isConnected()) {
return NETWORK_WIFI;
}
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobile != null && mobile.isConnected()) {
return NETWORK_MOBILE;
}
return NO_NETWORK;
}
}
其他
ToolUtils.java
/**
* 判斷輸入文字是否含有emoji
* @param string
* @return
*/
public static boolean isEmoji(String string) {
Pattern p = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(string);
return m.find();
}
/*public static boolean isEmoji1(String string) {
Pattern p = Pattern.compile("/[\u1F60-\u1F64]|[\u2702-\u27B0]|[\u1F68-\u1F6C]|[\u1F30-\u1F70]|[\u2600-\u26ff]/g");
Matcher m = p.matcher(string);
return m.matches();
}*/
/**
* 判斷是不是emoji字元
* @param codePoint
* @return
*/
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) ||
(codePoint == 0x9) ||
(codePoint == 0xA) ||
(codePoint == 0xD) ||
((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* 過濾emoji 或者 其他非文字型別的字元
* @param source
* @return
*/
public static String filterEmoji(String source) {
if (!isEmoji(source)) {
return source;//如果不包含,直接返回
}
//到這裡鐵定包含
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
} else {
}
}
if (buf == null) {
return source;//如果沒有找到 emoji表情,則返回源字串
} else {
if (buf.length() == len) {//這裡的意義在於儘可能少的toString,因為會重新生成字串
buf = null;
return source;
} else {
return buf.toString();
}
}
}
/**
* 顯示一個進度條
* @param activity
* @param customIndeterminateDrawable 自定義進度條載入圖片
* @return
*/
public static ProgressBar createProgressBar(Activity activity, Drawable customIndeterminateDrawable) {
// activity根部的ViewGroup,其實是一個FrameLayout
FrameLayout rootContainer = (FrameLayout) activity.findViewById(android.R.id.content);
// 給progressbar準備一個FrameLayout的LayoutParams
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// 設定對其方式為:螢幕居中對其
lp.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
ProgressBar progressBar = new ProgressBar(activity);
progressBar.setVisibility(View.GONE);
progressBar.setLayoutParams(lp);
// 自定義小菊花
if (customIndeterminateDrawable != null) {
progressBar.setIndeterminateDrawable(customIndeterminateDrawable);
}
// 將菊花新增到FrameLayout中
rootContainer.addView(progressBar);
return progressBar;
}
許可權相關 PermissionUtils.java
public static boolean checkPermission(String[] permissions,Activity activity,int requestCode){
List<String> needRequestPermissionList = ToolUtil.findDeniedPermissions(permissions,activity);
if (needRequestPermissionList != null && needRequestPermissionList.size() > 0){
ActivityCompat.requestPermissions(activity,needRequestPermissionList.toArray
(new String[needRequestPermissionList.size()]), PERMISSION_REQUESTCODE);
return true;
}
else return false;
}
public static boolean checkPermission(String[] permissions,Activity activity){
List<String> needRequestPermissionList = ToolUtil.findDeniedPermissions(permissions,activity);
if (needRequestPermissionList != null && needRequestPermissionList.size() > 0){
ActivityCompat.requestPermissions(activity,needRequestPermissionList.toArray
(new String[needRequestPermissionList.size()]), PERMISSION_REQUESTCODE);
return true;
}
else return false;
}
/**
*獲取許可權集中需要申請許可權的列表
* @param permissions
* @param activity
* @return
*/
public static List<String> findDeniedPermissions(String[] permissions,Activity activity) {
List<String> needRequestPermissionList = new ArrayList<>();
for (String perm : permissions){
if (ContextCompat.checkSelfPermission(TApplication.getContext(),perm) != PackageManager.PERMISSION_GRANTED){
needRequestPermissionList.add(perm);
}else{
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,perm)){
needRequestPermissionList.add(perm);
}
}
}
return needRequestPermissionList;
}
/**
* 檢測是否說有的許可權都有了
* @param grantResults
* @return
*/
public static boolean verifyPermission(int[] grantResults){
for (int result : grantResults){
if (result != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
public static void showMissingPermissionDialog(final Activity activity) {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(activity);
builder.setTitle(R.string.notifyMsg);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
activity.finish();
}
});
builder.setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSetting(activity);
}
});
builder.setCancelable(false);
builder.show();
}
public static void startAppSetting(Activity activity) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:"+activity.getPackageName()));
activity.startActivity(intent);
}