一、更新完自动安装
//跳转到安装页面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//第一个参数安装包路径,第二个参数固定
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
startActivity(intent);
二、防止Toast多次显示
private void showText(String msg) {
if (toast == null) {
toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.show();
}
三、倒计时功能(Thread+Handler)
//倒计时textview
private TextView mGetText;
private int recLen = 60;
//终止线程
private boolean flag;
final Handler handler = new Handler() {
// handle public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (recLen <= 60 && recLen > 0) {
recLen--;
mGetText.setText(recLen + "秒");
} else {
flag = false;
mGetText.setEnabled(true);
mGetText.setText("获取");
}
}
super.handleMessage(msg); }};
//点击倒计时的TextView时
flag = true;
recLen = 60;
if (recLen > 0) {
//防止多次点击
mGetText.setEnabled(false);
}
new Thread(new MyThread()).start();
//倒计时
public class MyThread implements Runnable {
@Override
public void run() {
while (flag) {
try {
Thread.sleep(1000); // sleep 1000ms
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
} catch (Exception e) {
}
}
}}
OnDestroy()中
@Override
protected void onDestroy() {
super.onDestroy();
//退出时关闭线程
flag = false;}
四、Md5,Sha1加密
/**
* sha1加密
* Created by zzz on 2016/10/27 0027.
*/
public class Sha1Util {
public static String encode(String text) {
//---拼接
StringBuffer sb = new StringBuffer();
try {
//获取sha1加密算法
MessageDigest instance = MessageDigest.getInstance("sha1");
//对字符窜进行加密 ---返回数组
byte[] digest = instance.digest(text.getBytes());
//遍历数组---转为16进制 32位的
for (byte b : digest) {
//获取字节低八位
int i = b & 0xff;
//转为16进制
String hexString = Integer.toHexString(i);
//如果i为1位--前方补0--凑成两位
if (hexString.length() < 2) {
hexString = "0" + hexString;
}
sb.append(hexString);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString();
}}
Md5加密只需将sha1换成md5即可
五、检查网络状态
/**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;}
六、获取客户端IP
public static String getIP() {
String IP = null;
StringBuilder IPStringBuilder = new StringBuilder();
try {
Enumeration networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterfaceEnumeration.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
Enumeration inetAddressEnumeration = networkInterface.getInetAddresses();
while (inetAddressEnumeration.hasMoreElements()) {
InetAddress inetAddress = inetAddressEnumeration.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() &&inetAddress.isSiteLocalAddress()) {
IPStringBuilder.append(inetAddress.getHostAddress().toString() );
}
}
}
} catch (SocketException ex) {
}
IP = IPStringBuilder.toString();
return IP;}
七、获取versionCode与versionName
private static int mVersionCode;
private static String mVersionName;
public static String getVersion(Context context) {
PackageManager manager = context.getPackageManager();
try {
PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
mVersionCode = packageInfo.versionCode;
mVersionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return mVersionName + "(" + mVersionCode + ")";}
八、替换String中的换行符、空格
position).getInfo().replaceAll("\\r|\\n", "").replaceAll(" ", "")
九、Edittext限制输入两位小数
mPriceEdit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
String temp = edt.toString();
int posDot = temp.indexOf(".");
//如果不输入,默认-1 从0开始
Log.d("tag", "posDot" + posDot);
if (posDot == 0) {
edt.clear();
}
Log.d("tag", "temp.length" + temp.length());
if (posDot != -1) {
if (temp.length() - posDot - 1 > 2) {
edt.delete(posDot + 3, posDot + 4);
}
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
});
十、隐藏系统输入法
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(BaoMingActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
十一、显示系统输入法
//弹起软键盘
//editeText先获取焦点
mBottomEdit.requestFocus();
InputMethodManager imm = (InputMethodManager) mBottomEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
十二、pop中软键盘将pop顶起
mPop_pwd.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
mPop_pwd.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
十三、Fragment中嵌套scrollView 当fragment切换回来时,scrollview有所滑动
在父布局文件中添加
android:focusable="true"
android:focusableInTouchMode="true"
十四、listview、gridview、recyclerview嵌套时出现显示不全(一般的解决方法)
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, expandSpec);
}
十五、app横竖屏切换时去掉系统的标题栏(时间 电量)
//由全屏变为半屏(显示电量)
//获得 WindowManager.LayoutParams
WindowManager.LayoutParams lp2 = getWindow().getAttributes();
//LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
lp2.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置属性
getWindow().setAttributes(lp2);
//不允许窗口扩展到屏幕之外 clear掉了
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//半屏变为全屏(隐藏电量)
//获得 WindowManager.LayoutParams 属性对象
WindowManager.LayoutParams lp = getWindow().getAttributes();
//直接对它flags变量操作 LayoutParams.FLAG_FULLSCREEN 表示设置全屏
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置属性
getWindow().setAttributes(lp);
//意思大致就是 允许窗口扩展到屏幕之外
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
十六、修改布局背景色时全局背景色都发生变化
mSearchLayout.getBackground().mutate().setAlpha(255);
十七、webView播放网页视频退出后仍然播放
onPause方法中
mWebView.reload();
十八、ScrollView嵌套webView时,点击webView会自动滚动,使webView填满屏幕
//在webView的父布局中添加
//descendantFocusability属性的作用是当一个view获取焦点时,定义viewGroup和其子控件两者之间的关系。而blocksDescendants是viewgroup会覆盖子类控件而直接获得焦点。
android:descendantFocusability="blocksDescendants"
十九、禁止recyclerview滑动
LinearLayoutManager manager = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
二十、SDKManager配置
二十一、解决Scrollview在滑动时的点击回到顶部
mScroll.scrollTo(0, 0);
mScroll.smoothScrollTo(0, 0);
二十二、解决App在第一次启动时白屏时间过长。
由于AndroidStudio2.0之后多了Instant Run功能,该功能用来提高开发效率,然而为了能让Instant Run可以正常工作,App在首次启动时会初始化相关工作,从而出现白屏现象。但是用户并不理解这些,只会让用户体验不好。
解决方法:将App打包成release包,就会解决白屏现象。
进一步优化
Theme中
// 可以让程序在初始化时是透明的
- true
- true
二十三、Android5.0以上 webView嵌套Https视频无法播放
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}