个人收藏的Android常用工具类,每次做项目,一些常用的工具类总要重写一篇,烦死了,终于还是建了库,收藏这些常用的。
#Gradle依赖
compile 'com.wuxiaolong.androidutils:androidutils:1.0.6'
写在基类,然后每个Activity都去继承
public class BaseActivity extends AppCompatActivity {
public ActivityManagerUtil activityManagerUtil;
public Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mActivity = this;
activityManagerUtil = ActivityManagerUtil.getInstance();
activityManagerUtil.pushOneActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//结束Activity&从栈中移除该Activity
activityManagerUtil.popOneActivity(this);
}
}
需要完全退出
activityManagerUtil.appExit();
只要在Application 初始化即可
public class AndroidUtilsApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//崩溃处理
CrashHandlerUtil crashHandlerUtil = CrashHandlerUtil.getInstance();
crashHandlerUtil.init(this);
crashHandlerUtil.setCrashTip("很抱歉,程序出现异常,即将退出!");
}
}
String unixTimestamp2BeijingTime(Object millisecond, String format)
long beijingTime2UnixTimestamp(String beijingTime, String format)
图片压缩类
算术类,包括加减乘除
工具类集合,不好单独起名都放在AppUtils里
String decimalFormat(double s, String format)
void hideSoftInput(Activity activity)
byte[] bitmap2Bytes(Bitmap bitmap)
String md5(String plainText)
void installAPK(Context context, String path)
void actionCall(Context context, String phone)
void actionDial(Context context, String phone)
int getScreenWidth(Context context)
int getScreenHeight(Context context)
int dip2px(Context context, float dpValue)
int px2dip(Context context, float pxValue)
int sp2px(Context context, float spValue)
int px2sp(Context context, float pxValue)
通过Url获取绝对路径
String getAbsolutePath(final Context context, final Uri uri)
步骤1:
AndroidManifest注册
<receiver android:name="com.wuxiaolong.androidutils.library.DownloadUtil$DownloadManagerReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
步骤2: 调用DownloadManager下载
DownloadUtil downloadUtil = new DownloadUtil(content, downloadUrl);
//下载显示名字,不能是中文
downloadUtil.setDownloadFileName("apkName" + System.currentTimeMillis() + ".apk");
downloadUtil.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadUtil.start();
log打印类
LogUtil.d("");
String phoneNoHide(String phone)
String cardIdHide(String cardId)
String idHide(String id)
boolean checkVehicleNo(String vehicleNo)
boolean checkEmail(String email)
boolean checkIdCard(String idCard)
boolean checkMobile(String mobile)
boolean checkPhone(String phone)
boolean checkDecimals(String decimals)
SharedPreferences工具类
void setString(Context context, final String key,final String value)
时间工具类
String getVersionName(Context context)
int getVersionCode(Context context)
多媒体工具类
void startActivityForCamera(Activity activity, int requestCode, Uri outputUri)
void startActivityForCamera(Activity activity, int requestCode)
void startActivityForGallery(Activity activity, int requestCode)
void startActivityForImageCut(Activity activity, int requestCode,
Uri inputUri, Uri outputUri,
int width, int height)
Uri工具类
我的微信公众号:吴小龙同学,欢迎关注交流。
Android技术交流群-剩者为王⑤群:590967484