小知识点积累
程序员文章站
2024-02-25 15:50:57
...
1.android默认的user-agent获取
protected final String getDefaultUserAgent() {
String agent = System.getProperty("http.agent");
return agent != null ? agent : ("Java" + System.getProperty("java.version"));
}
2.判断应用是否处于前台
public class MyApplication extends Application {
private int visibleActivityCount;
@SuppressLint("NewApi")
@Override
public void onCreate() {
super.onCreate();
this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityStopped(Activity activity) {
visibleActivityCount--;
if(visibleActivityCount==0){
//应用处于后台
}
}
@Override
public void onActivityStarted(Activity activity) {
visibleActivityCount++;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
});
}
}
3.利用thread+handler解决asynctask在华为手机上的bug
public class AttachDownloadThread implements Runnable {
private DownloadThreadCallback callback;
private String path;
private String url;
private Handler mHandler;
private final int PREACTION =11;
private final int POSTACTION = 12;
public AttachDownloadThread(String url,String path,DownloadThreadCallback callback){
this.url = url;
this.path = path;
this.callback = callback;
mHandler = new MHandler();
}
@Override
public void run() {
sendMsg(PREACTION);
File file = HttpUtil.getDatas(this.url, this.path);
sendMsg(POSTACTION,file);
}
private void sendMsg(int what){
Message msg = mHandler.obtainMessage();
msg.what = what;
mHandler.sendMessage(msg);
}
private void sendMsg(int what,Object obj){
Message msg = mHandler.obtainMessage();
msg.what = what;
msg.obj = obj;
mHandler.sendMessage(msg);
}
public interface DownloadThreadCallback{
void preDownload();
void postDownload(File file);
}
private class MHandler extends Handler{
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case PREACTION:
Log.e("xihe","callback pre");
callback.preDownload();
break;
case POSTACTION:
Log.e("xihe","callback post");
callback.postDownload((File)msg.obj);
break;
}
super.handleMessage(msg);
}
}
}
3.TimeUtil
public class TimeUtil {
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 根据时间转换成相应的字符串提示语(消息中心)
*
* @param context
* @param date
* @return
*/
public static String timeTransform(Context context, Date date) {
if (date == null) {
return "";
}
long time = date.getTime();
String s = null;
long current = System.currentTimeMillis();
long diff = (current - time) / 1000;
int hour = 60 * 60;
int day = hour * 24;
int twoDay = day * 2;
int eightDay = day * 8;
if (diff <= day) {
s = formatDateToHM(time);
} else if (diff >= day && diff < twoDay) {
s = context.getString(R.string.message_yestoday);
} else if (diff >= twoDay && diff < eightDay) {
String[] weeks = context.getResources().getStringArray(
R.array.message_day_of_week);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
s = weeks[w];
} else if (diff >= eightDay) {
s = formatDateToYMD(time);
} else {
s = formatDateToYMDHM(time);
}
return s;
}
/**
* 根据时间转换成相应的字符串提示语
*
* @param time
* 时间(单位:秒)
* @return
*/
public static String timeTransform(long time) {
String s = "刚刚";
if (time < 60) {
s = "刚刚";
} else if (time > 59 && time < 3600) {
s = time / 60 + "分钟前";
} else if (time > 3599 && time < 3600 * 24) {
s = time / 3600 + "小时前";
} else if (time > 86399) {
s = time / 86400 + "天前";
}
return s;
}
/**
* 根据时间转换成相应的字符串提示语(消息中心)
*
* @param time
* 传入time即可new Date().getTime()
* @return
*/
public static String timeTransform(Context context, long time) {
String s = null;
String[] tips = context.getResources().getStringArray(
R.array.message_reminder_tip);
long current = System.currentTimeMillis();
long diff = (current - time) / 1000;
int second = 60;
int minute = second * 60;
int hour = minute * 60;
int day = hour * 24;
if (diff < second) {
s = tips[0];
} else if (diff >= second && diff < minute) {
s = diff / second + tips[1];
} else if (diff >= minute && diff < hour) {
s = diff / minute + tips[2];
} else if (diff >= day) {
s = diff / hour + tips[3];
} else {
s = tips[0];
}
return s;
}
/**
* 格式化 秒数: 00:00 倒计时
*
* @param time
* @return
*/
public static String phpTimeTransform(long time) {
long diff = System.currentTimeMillis() / 1000 - time;
return timeTransform((int) diff);
}
/**
* 格式化 秒数: 00:00
*/
public static String formatSecond(long second) {
long h = second / (60 * 60);
long m = (second - 60 * h) / 60;
long s = second % 60;
String result = "";
if (h > 0) {
result = String.format("%02d:%02d:%02d", h, m, s);
} else {
result = String.format("%02d:%02d", m, s);
}
return result;
}
/**
* 格式化 毫秒数: 00:00
*/
public static String formatMillisSecond(long l) {
long h = l / (1000 * 60 * 60) % 60;
long m = l / (1000 * 60) % 60;
long s = l / 1000 % 60;
if (h > 0) {
return String.format("%02d:%02d:%02d", h, m, s);
} else {
return String.format("%02d:%02d", m, s);
}
}
/**
* 格式化 MM-dd HH:mm
*
* @param time
* 时间戳,单位是毫秒
*/
public static String formatDate(long time) {
if (time <= 0) {
return "";
}
Date nowTime = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
return format.format(nowTime);
}
/**
* 格式化 MM-dd HH:mm
*
* @param time
* 时间戳,单位是秒
*/
public static String formatDate(int time) {
if (time <= 0) {
return "";
}
Date nowTime = new Date(time * 1000L);
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
return format.format(nowTime);
}
/**
* 格式化 YY-MM-dd HH:mm
*
* @param time
* @return
*/
public static String formatDateToYMDHM(long time) {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(time);
return date;
}
/**
* 格式化 YY-MM-dd HH:mm
*
* @param time
* @return
*/
public static String formatDateToYMD(long time) {
String date = new SimpleDateFormat("yyyy-MM-dd").format(time);
return date;
}
/**
* 格式化 HH:mm
*
* @param time
* @return
*/
public static String formatDateToHM(long time) {
String date = new SimpleDateFormat("HH:mm").format(time);
return date;
}
/**
* 格式化 HH:mm
*
* @param time
* @return
*/
public static String formatDateToHMS(long time) {
String date = new SimpleDateFormat("HH:mm:ss").format(time);
return date;
}
public static Date str2Date(String str, String format) {
if (str == null || str.length() == 0) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
}
public class TimeUnit {
public final static String LONGEST_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
public final static String LONG_FORMAT = "yyyy-MM-dd HH:mm:ss";
public final static String SHORT_FORMAT = "yyyy-MM-dd";
public final static String TIME_FORMAT = "HH:mm:ss";
private static SimpleDateFormat formatter = new SimpleDateFormat();
// ///////////////////////////////////////////////////////////////
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss.SSS
*/
public static Date getNowDateLongest() {
return getNowDate(LONGEST_FORMAT);
}
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
return getNowDate(LONG_FORMAT);
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static Date getNowDateShort() {
return getNowDate(SHORT_FORMAT);
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static Date getNowTimeShort() {
return getNowDate(TIME_FORMAT);
}
/**
* 获取现在时间
*
* @param timeFormat
* 返回时间格式
*/
public static Date getNowDate(String timeFormat) {
Date currentTime = new Date();
Date currentTime_2 = null;
synchronized (formatter) {
formatter.applyPattern(timeFormat);
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(0);
currentTime_2 = formatter.parse(dateString, pos);
}
return currentTime_2;
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 获取现在时间
*
* @return 返回字符串格式 yyyy-MM-dd HH:mm:ss.SSS
*/
public static String getStringDateLongest() {
return getStringDate(LONGEST_FORMAT);
}
/**
* 获取现在时间
*
* @return 返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
return getStringDate(LONG_FORMAT);
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
return getStringDate(SHORT_FORMAT);
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
return getStringDate(TIME_FORMAT);
}
/**
* 获取现在时间
*
* @param 返回字符串格式
*/
public static String getStringDate(String timeFormat) {
Date currentTime = new Date();
String dateString = null;
synchronized (formatter) {
formatter.applyPattern(timeFormat);
dateString = formatter.format(currentTime);
}
return dateString;
}
// //////////////////////////////////////////////////////////////////////////////
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss.SSS
*
* @param strDate
* @return
*/
public static Date strToLongDateLongest(String strDate) {
return strToDate(strDate, LONGEST_FORMAT);
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToLongDate(String strDate) {
return strToDate(strDate, LONG_FORMAT);
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToShortDate(String strDate) {
return strToDate(strDate, SHORT_FORMAT);
}
/**
* 将时间格式字符串转换为时间 HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToTimeDate(String strDate) {
return strToDate(strDate, TIME_FORMAT);
}
/**
* 按指定的时间格式字符串转换为时间
*
* @param strDate
* @param timeFormat
* @return
*/
public static Date strToDate(String strDate, String timeFormat) {
Date strtodate = null;
synchronized (formatter) {
formatter.applyPattern(timeFormat);
ParsePosition pos = new ParsePosition(0);
strtodate = formatter.parse(strDate, pos);
}
return strtodate;
}
// ///////////////////////////////////////////////////////////////////////////////
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss.SSS
*
* @param dateDate
* @return
*/
public static String dateToLongestStr(Date dateDate) {
return dateToStr(dateDate, LONGEST_FORMAT);
}
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToLongStr(Date dateDate) {
return dateToStr(dateDate, LONG_FORMAT);
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static String dateToShortStr(Date dateDate) {
return dateToStr(dateDate, SHORT_FORMAT);
}
/**
* 将时间格式字符串转换为时间 HH:mm:ss
*
* @param strDate
* @return
*/
public static String dateToTimeStr(Date dateDate) {
return dateToStr(dateDate, TIME_FORMAT);
}
/**
* 按指定的时间格式时间转换为字符串
*
* @param dateDate
* @param timeFormat
* @return
*/
public static String dateToStr(Date dateDate, String timeFormat) {
String dateString = null;
synchronized (formatter) {
formatter.applyPattern(timeFormat);
dateString = formatter.format(dateDate);
}
return dateString;
}
public static String LongToStr(long m, String timeFormat) {
String dateString = null;
synchronized (formatter) {
formatter.applyPattern(timeFormat);
dateString = formatter.format(new Date(m));
}
return dateString;
}
/**
* 将日期格式的字符串转换为长整型
*
* @param date
* @param format
* @return
*/
public static long convert2long(String date, String format) {
SimpleDateFormat sf = new SimpleDateFormat(format);
try {
return sf.parse(date).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0L;
}
}
上一篇: Linux 快捷操作集群脚本