-
return null;
-
}
-
/**
-
* 移除某个key值已经对应的值
-
* @param context
-
* @param key
-
*/
-
public static void remove(Context context, String key)
-
{
-
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
-
Context.MODE_PRIVATE);
-
SharedPreferences.Editor editor = sp.edit();
-
editor.remove(key);
-
SharedPreferencesCompat.apply(editor);
-
}
-
/**
-
* 清除所有数据
-
* @param context
-
*/
-
public static void clear(Context context)
-
{
-
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
-
Context.MODE_PRIVATE);
-
SharedPreferences.Editor editor = sp.edit();
-
editor.clear();
-
SharedPreferencesCompat.apply(editor);
-
}
-
/**
-
* 查询某个key是否已经存在
-
* @param context
-
* @param key
-
* @return
-
*/
-
public static boolean contains(Context context, String key)
-
{
-
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
-
Context.MODE_PRIVATE);
-
return sp.contains(key);
-
}
-
/**
-
* 返回所有的键值对
-
*
-
* @param context
-
* @return
-
*/
-
public static Map<String, ?> getAll(Context context)
-
{
-
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
-
Context.MODE_PRIVATE);
-
return sp.getAll();
-
}
-
/**
-
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
-
*
-
* @author zhy
-
*
-
*/
-
private static class SharedPreferencesCompat
-
{
-
private static final Method sApplyMethod = findApplyMethod();
-
/**
-
* 反射查找apply的方法
-
*
-
* @return
-
*/
-
@SuppressWarnings({ “unchecked”, “rawtypes” })
-
private static Method findApplyMethod()
-
{
-
try
-
{
-
Class clz = SharedPreferences.Editor.class;
-
return clz.getMethod(“apply”);
-
} catch (NoSuchMethodException e)
-
{
-
}
-
return null;
-
}
-
/**
-
* 如果找到则使用apply执行,否则使用commit
-
*
-
* @param editor
-
*/
-
public static void apply(SharedPreferences.Editor editor)
-
{
-
try
-
{
-
if (sApplyMethod != null)
-
{
-
sApplyMethod.invoke(editor);
-
return;
-
}
-
} catch (IllegalArgumentException e)
-
{
-
} catch (IllegalAccessException e)
-
{
-
} catch (InvocationTargetException e)
-
{
-
}
-
editor.commit();
-
}
-
}
-
}
对SharedPreference的使用做了建议的封装,对外公布出put,get,remove,clear等等方法;
注意一点,里面所有的commit操作使用了SharedPreferencesCompat.apply进行了替代,目的是尽可能的使用apply代替commit
首先说下为什么,因为commit方法是同步的,并且我们很多时候的commit操作都是UI线程中,毕竟是IO操作,尽可能异步;
所以我们使用apply进行替代,apply异步的进行写入;
但是apply相当于commit来说是new API呢,为了更好的兼容,我们做了适配;
SharedPreferencesCompat也可以给大家创建兼容类提供了一定的参考~~
==================================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import android.content.Context;
-
import android.util.TypedValue;
-
/**
-
* 常用单位转换的辅助类
-
*
-
*
-
*
-
*/
-
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);
-
}
-
}
====================================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import java.io.File;
-
import android.os.Environment;
-
import android.os.StatFs;
-
/**
-
* SD卡相关的辅助类
-
*
-
*
-
*
-
*/
-
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();
-
}
-
}
===================================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.Rect;
-
import android.util.DisplayMetrics;
-
import android.view.View;
-
import android.view.WindowManager;
-
/**
-
* 获得屏幕相关的辅助类
-
*
-
*
-
*
-
*/
-
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;
-
}
-
}
========================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import android.content.Context;
-
import android.content.pm.PackageInfo;
-
import android.content.pm.PackageManager;
-
import android.content.pm.PackageManager.NameNotFoundException;
-
/**
-
* 跟App相关的辅助类
-
*
-
*
-
*
-
*/
-
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;
-
}
-
}
=====================================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import android.content.Context;
-
import android.view.inputmethod.InputMethodManager;
-
import android.widget.EditText;
-
/**
-
* 打开或关闭软键盘
-
*
-
* @author zhy
-
*
-
*/
-
public class KeyBoardUtils
-
{
-
/**
-
* 打卡软键盘
-
*
-
* @param mEditText
-
* 输入框
-
* @param mContext
-
* 上下文
-
*/
-
public static void openKeybord(EditText mEditText, Context mContext)
-
{
-
InputMethodManager imm = (InputMethodManager) mContext
-
.getSystemService(Context.INPUT_METHOD_SERVICE);
-
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
-
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
-
InputMethodManager.HIDE_IMPLICIT_ONLY);
-
}
-
/**
-
* 关闭软键盘
-
*
-
* @param mEditText
-
* 输入框
-
* @param mContext
-
* 上下文
-
*/
-
public static void closeKeybord(EditText mEditText, Context mContext)
-
{
-
InputMethodManager imm = (InputMethodManager) mContext
-
.getSystemService(Context.INPUT_METHOD_SERVICE);
-
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
-
}
-
}
================================================================================
[java] view plain copy
-
package com.zhy.utils;
-
import android.app.Activity;
-
import android.content.ComponentName;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.net.ConnectivityManager;
-
import android.net.NetworkInfo;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
最后
答应大伙的备战金三银四,大厂面试真题来啦!
这份资料我从春招开始,就会将各博客、论坛。网站上等优质的Android开发中高级面试题收集起来,然后全网寻找最优的解答方案。每一道面试题都是百分百的大厂面经真题+最优解答。包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
给文章留个小赞,就可以免费领取啦~
《960全网最全Android开发笔记》
《379页Android开发面试宝典》
包含了腾讯、百度、小米、阿里、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。熟悉本文中列出的知识点会大大增加通过前两轮技术面试的几率。
如何使用它?
1.可以通过目录索引直接翻看需要的知识点,查漏补缺。
2.五角星数表示面试问到的频率,代表重要推荐指数
《507页Android开发相关源码解析》
只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。
真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。
腾讯、字节跳动、阿里、百度等BAT大厂 2020-2021面试真题解析
资料收集不易,如果大家喜欢这篇文章,或者对你有帮助不妨多多点赞转发关注哦。文章会持续更新的。绝对干货!!!
8)]
[外链图片转存中…(img-RBqorGMg-1710567643998)]
[外链图片转存中…(img-lv9H0xix-1710567643998)]
[外链图片转存中…(img-wo4lpVUG-1710567643999)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-tcjN9iBx-1710567643999)]
最后
答应大伙的备战金三银四,大厂面试真题来啦!
这份资料我从春招开始,就会将各博客、论坛。网站上等优质的Android开发中高级面试题收集起来,然后全网寻找最优的解答方案。每一道面试题都是百分百的大厂面经真题+最优解答。包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
给文章留个小赞,就可以免费领取啦~
《960全网最全Android开发笔记》
[外链图片转存中…(img-Kk8dLuIr-1710567643999)]
《379页Android开发面试宝典》
包含了腾讯、百度、小米、阿里、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。熟悉本文中列出的知识点会大大增加通过前两轮技术面试的几率。
如何使用它?
1.可以通过目录索引直接翻看需要的知识点,查漏补缺。
2.五角星数表示面试问到的频率,代表重要推荐指数
[外链图片转存中…(img-MkDtOyik-1710567643999)]
《507页Android开发相关源码解析》
只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。
真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。
[外链图片转存中…(img-tEnlwWhg-1710567644000)]
腾讯、字节跳动、阿里、百度等BAT大厂 2020-2021面试真题解析
[外链图片转存中…(img-pNZeM3ib-1710567644000)]
资料收集不易,如果大家喜欢这篇文章,或者对你有帮助不妨多多点赞转发关注哦。文章会持续更新的。绝对干货!!!