双击退出应该这样操作
private static Boolean isExit = false;
private void exitBy2Click() {
Timer tExit = null;
if (isExit == false) {
isExit = true; // 准备退出
ExtUtils.shortToast(this, "双击退出APP");
tExit = new Timer();
tExit.schedule(new TimerTask() {
@Override
public void run() {
isExit = false;
}
}, 2000);
} else {
finish();
}
}
版本更新
private void getVersion(final String VERSION_CODE) {
RequestManager.requestData(2, Request.Method.GET, Urls.URL_PREFIX + "update.jsp?ver=apk", BaseData.class, null, "getVersion", new Response.Listener<BaseData>() {
@Override
public void onResponse(BaseData response) {
if (response != null) {
String Code = ExtUtils.cutStringtoBefore(ExtUtils.cutStringtoAfter(response.data, "@"), "@");
int myVersionCode = Integer.parseInt(VERSION_CODE.replace(".", ""));
int VersionCode = Integer.parseInt(Code.replace(".", ""));
if (myVersionCode < VersionCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(Main_FragmentManagers.this);
final AlertDialog dialog = builder.create();
builder.setView(View.inflate(Main_FragmentManagers.this, R.layout.layout_apk_updata_notic, null));
dialog.show();
Window window = dialog.getWindow();
window.setContentView(R.layout.layout_apk_updata_notic);
final TextView content = (TextView) window.findViewById(R.id.txt_content);
final Button cancel = (Button) window.findViewById(R.id.cancel);
final Button ok = (Button) window.findViewById(R.id.ok);
content.setText(ExtUtils.cutStringtoBefore(response.data, "@"));
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//下载apk
ApkDownloadUtil util = new ApkDownloadUtil(Main_FragmentManagers.this);
util.download();
dialog.dismiss();
}
});
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
ExtUtils.errorLog("--MainFragment--VERSION_CODE--->", "" + error);
RequestManager.cancelAll("getVersion");
}
});
}
版本更新工具类
public class ApkDownloadUtil {
//下载APP的链接网址
private static final String CHUNYU_DOWNLOAD_URL = "http://www.hefd.cn:11888/mobile_server/flfg.apk";
//Apk的类型
private static final String APK_TYPE = "application/vnd.android.package-archive";
//Apk的默认名称
private static final String DEFAULT_FILE_NAME = "hjkk.apk";
//默认安装页面的App名称
private static final String DEFAULT_TITLE = "mmm";
private Context mContext; // 下载进行
private String mUrl; // URL地址
private String mFileName; // 文件名
private String mTitle; // 通知栏标题
private DownloadManager mDownloadManager; // 下载管理器
private long mDownloadId; // 下载ID
// 下载完成的接收器
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadId == mDownloadId) {
installApp(mDownloadId);
destroyArgs();
}
}
};
/**
* 默认构造器, 下载
* @param context 上下文
*/
public ApkDownloadUtil(Context context) {
this(context, CHUNYU_DOWNLOAD_URL, DEFAULT_FILE_NAME, DEFAULT_TITLE);
}
/**
* 参数构造器, 下载其他文件 * *
* @param context 活动 *
* @param url URL *
* @param fileName 存储文件名
* @param title 通知栏标题
*/
public ApkDownloadUtil(Context context, String url, String fileName, String title) {
mContext = context;
mUrl = url;
mFileName = fileName;
mTitle = title;
initArgs();
}
/**
* 下载文件
*/
public void download() {
// 设置下载Url
Uri resource = Uri.parse(mUrl);
DownloadManager.Request request = new DownloadManager.Request(resource);
// 设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUrl));
request.setMimeType(mimeString);
// 下载完成时在进度条提示
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// 存储sdcard目录的/jklkn/download文件夹
request.setDestinationInExternalPublicDir("/yuoiyoi/download/", mFileName);
request.setTitle(mTitle);
// 开始下载
mDownloadId = mDownloadManager.enqueue(request);
}
// 初始化
private void initArgs() {
mDownloadManager = (DownloadManager) mContext.getSystemService((Context.DOWNLOAD_SERVICE));
mContext.registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
// 析构
private void destroyArgs() {
mContext.unregisterReceiver(mReceiver);
}
// 安装App
private void installApp(long downloadId) {
Intent install = new Intent(Intent.ACTION_VIEW);
Uri downloadFileUri = mDownloadManager.getUriForDownloadedFile(downloadId);
install.setDataAndType(downloadFileUri, APK_TYPE);
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
mContext.startActivity(install);
}
}
字符串工具类
public class ExtUtils {
/**
* 字符串判断是否为空
*
* @param s
* @return
*/
public static boolean isEmpty(String s) {
if (null == s)
return true;
if (s.length() == 0)
return true;
if (s.trim().length() == 0)
return true;
return false;
}
/**
* 字符串判断是否不为空
*
* @param s
* @return
*/
public static boolean isNotEmpty(String s) {
return !isEmpty(s);
}
/**
* 判断是否连接网络
*
* @param context
* @return
*/
public static boolean isConnected(Context context) {
ConnectivityManager conn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conn.getActiveNetworkInfo();
return (info != null && info.isConnected());
}
/**
* 短Toast
*
* @param context
* @param s
*/
public static void shortToast(Context context, String s) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.makeText(context, s, Toast.LENGTH_SHORT).show();
}
/**
* 长Toast
*
* @param context
* @param s
*/
public static void longToast(Context context, String s) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.makeText(context, s, Toast.LENGTH_LONG).show();
}
/**
* 错误日志Log
*
* @param key
* @param value
*/
public static void errorLog(String key, String value) {
Log.e("-----------" + key + "-----------", value);
}
/**
* 信息日志Log
*
* @param key
* @param value
*/
public static void infoLog(String key, String value) {
Log.i("-----------" + key + "-----------", value);
}
/**
* 将数字格式化,以整数位每三位用“,”隔开,并保留为小数
*
* @param price
* @return
*/
public static String RoundTwoDecimal(double price) {
/*保留两位小数点*/
/*
NumberFormat nf= new DecimalFormat("0.00");
String s = nf.format(price);
*/
BigDecimal bd = new BigDecimal(price);
bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
StringBuilder sb = new StringBuilder();
int index = (bd + "").lastIndexOf(".");
String str1 = (bd + "").substring(0, index);
// 取出小数点前面的字符串
String str2 = (bd + "").substring(index);
// 存放小数点后面的内容
str1 = new StringBuilder(str1).reverse().toString();
// 将字符串颠倒过来
int size = (str1.length() % 3 == 0) ? (str1.length() / 3) : (str1
.length() / 3 + 1);
// 将字符串每三位分隔开
for (int i = 0; i < size - 1; i++) {
sb.append(str1.substring(i * 3, i * 3 + 3) + ",");
}
// 前n-1段都用逗号连接上
str1 = sb.toString() + str1.substring((size - 1) * 3);
//将前n-1段和第n段连接在一起
str1 = new StringBuilder(str1).reverse().toString();
//连接上原来小数点后面的数
return str1 + str2;
}
public static String RoundTwoDecimals(double price) {
NumberFormat nf = new DecimalFormat("0");
String s = nf.format(100 - Integer.parseInt(nf.format(price)));
return s;
}
/**
* 不允许输入空格
*
* @param string
* @return
*/
public static String Trim(String string) {
return string.trim();
}
/**
* 首尾字符替换
*
* @param str
* @param head 需要替换的首字符
* @param end 需要替换的尾字符
* @return
*/
public String replaceStr(String str, String head, String end) {
String headStr = str.replace(str.substring(0, 1), head);
String endStr = headStr.replace(str.substring(str.length() - 1, str.length()), end);
return endStr;
}
/**
* 字符串转码
*
* @param str
* @return
*/
public static String toURLEncoder(String str) throws UnsupportedEncodingException {
return null != str && !"".equals(str) ? URLEncoder.encode(URLEncoder.encode(str, "UTF-8"), "UTF-8") : str;
}
/**
* 转码转中文
*
* @param str
* @return
*/
public static String toURLDecoder(String str) throws UnsupportedEncodingException {
return null != str && !"".equals(str) ? URLDecoder.decode(URLDecoder.decode(str, "UTF-8"), "UTF-8") : str;
}
//测试专用打印
public static void getDataInfo(String className, String inputStearm) {
System.out.println(className + "------->" + inputStearm);
}
/**
* 截取指定字符之前的字符串
*
* @param str 字符串
* @param flag 需要从哪一个字符截取
* @return
*/
public static String cutStringtoBefore(String str, String flag) {
if (isNotEmpty(str) && isNotEmpty(flag)) {
if (str.indexOf(flag) < 0) return "";
else {
String newStr = str.substring(0, str.indexOf(flag));
return newStr;
}
} else return "";
}
/**
* 截取指定字符之后的字符串
*
* @param str 字符串
* @param flag 需要从哪一个字符截取
* @return
*/
public static String cutStringtoAfter(String str, String flag) {
if (isNotEmpty(str) && isNotEmpty(flag)) {
if (str.indexOf(flag) < 0) return "";
else {
String newStr = str.substring(str.indexOf(flag) + 1, str.length());
return newStr;
}
} else return "";
}
private static String regex = "<(span|SPAN)[\\s ]+style='back.*?>";
private static String font = "<font color=\"red\">";
public static String changeData(String jsonStr, String fontStr, String color) {
if (fontStr == null || "".equals(fontStr)) {
fontStr = font;
}
if (color != null && !"".equals(color)) {
fontStr = font.replace("red", color);
}
return jsonStr == null || "".equals(jsonStr) ? jsonStr : jsonStr.replaceAll(regex, fontStr).replace("</span>", "</font>");
}
public static String changeData(String jsonStr) {
return changeData(jsonStr, null, null);
}
public static String changeDataToColor(String jsonStr, String color) {
return changeData(jsonStr, null, color);
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* @author GoldDreams
* @Time 2017/7/12 0012 :15:26
* fuction 获得手机序列号imei
*/
public static String getDeviceId(Context context) {
StringBuilder deviceId = new StringBuilder();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if (isNotEmpty(imei)) {
deviceId.append(imei);
Log.i("getDeviceId : ", deviceId.toString());
return deviceId.toString();
}
return "错误!!!";
}
/**
* @author GoldDreams
* @Time 2017/7/12 0012 :15:26
* fuction 判断网络是否可用
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
* @author GoldDreams
* @Time 2017/7/12 0012 :15:26
* fuction 判断网络状态
*/
public static String getNetworkStatusCode(VolleyError error) {
if (error.networkResponse.statusCode == 408) {
return "网络超时...";
} else if (error.networkResponse.statusCode == 404) {
return "服务器不可用...";
} else if (error.networkResponse.statusCode == 500) {
return "服务器错误";
}
return "" + error.networkResponse.statusCode;
}
}
分享到更多
private void shareMore() {
Intent Intents = new Intent(Intent.ACTION_SEND);
Intents.setType("text/plain");
Intents.putExtra(Intent.EXTRA_TEXT, "gnfg\nhttp://www.gmihjhu.cn:11888/mobile_server/downflfg.html?key=d1503399183481");
startActivity(Intent.createChooser(Intents, "分享"));
}