一橄务、更新完自動(dòng)安裝
//跳轉(zhuǎn)到安裝頁(yè)面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//第一個(gè)參數(shù)安裝包路徑幔托,第二個(gè)參數(shù)固定
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();
}
三蜂挪、倒計(jì)時(shí)功能(Thread+Handler)
//倒計(jì)時(shí)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); }};
//點(diǎn)擊倒計(jì)時(shí)的TextView時(shí)
flag = true;
recLen = 60;
if (recLen > 0) {
//防止多次點(diǎn)擊
mGetText.setEnabled(false);
}
new Thread(new MyThread()).start();
//倒計(jì)時(shí)
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();
//退出時(shí)關(guān)閉線程
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");
//對(duì)字符竄進(jìn)行加密 ---返回?cái)?shù)組
byte[] digest = instance.digest(text.getBytes());
//遍歷數(shù)組---轉(zhuǎn)為16進(jìn)制 32位的
for (byte b : digest) {
//獲取字節(jié)低八位
int i = b & 0xff;
//轉(zhuǎn)為16進(jìn)制
String hexString = Integer.toHexString(i);
//如果i為1位--前方補(bǔ)0--湊成兩位
if (hexString.length() < 2) {
hexString = "0" + hexString;
}
sb.append(hexString);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString();
}}
Md5加密只需將sha1換成md5即可
五、檢查網(wǎng)絡(luò)狀態(tài)
/**
* 網(wǎng)絡(luò)是否可用
*
* @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<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterfaceEnumeration.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
Enumeration<InetAddress> 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限制輸入兩位小數(shù)
mPriceEdit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
String temp = edt.toString();
int posDot = temp.indexOf(".");
//如果不輸入,默認(rèn)-1 從0開(kāi)始
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) {
}
});
十驳糯、隱藏系統(tǒng)輸入法
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(BaoMingActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
十一篇梭、顯示系統(tǒng)輸入法
//彈起軟鍵盤
//editeText先獲取焦點(diǎn)
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 當(dāng)fragment切換回來(lái)時(shí)恬偷,scrollview有所滑動(dòng)
在父布局文件中添加
android:focusable="true"
android:focusableInTouchMode="true"
十四、listview隧枫、gridview喉磁、recyclerview嵌套時(shí)出現(xiàn)顯示不全(一般的解決方法)
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, expandSpec);
}
十五谓苟、app橫豎屏切換時(shí)去掉系統(tǒng)的標(biāo)題欄(時(shí)間 電量)
//由全屏變?yōu)榘肫粒@示電量)
//獲得 WindowManager.LayoutParams
WindowManager.LayoutParams lp2 = getWindow().getAttributes();
//LayoutParams.FLAG_FULLSCREEN 強(qiáng)制屏幕狀態(tài)條欄彈出
lp2.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
//設(shè)置屬性
getWindow().setAttributes(lp2);
//不允許窗口擴(kuò)展到屏幕之外 clear掉了
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//半屏變?yōu)槿粒[藏電量)
//獲得 WindowManager.LayoutParams 屬性對(duì)象
WindowManager.LayoutParams lp = getWindow().getAttributes();
//直接對(duì)它flags變量操作 LayoutParams.FLAG_FULLSCREEN 表示設(shè)置全屏
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//設(shè)置屬性
getWindow().setAttributes(lp);
//意思大致就是 允許窗口擴(kuò)展到屏幕之外
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
十六、修改布局背景色時(shí)全局背景色都發(fā)生變化
mSearchLayout.getBackground().mutate().setAlpha(255);
十七协怒、webView播放網(wǎng)頁(yè)視頻退出后仍然播放
onPause方法中
mWebView.reload();
十八涝焙、ScrollView嵌套webView時(shí),點(diǎn)擊webView會(huì)自動(dòng)滾動(dòng)孕暇,使webView填滿屏幕
//在webView的父布局中添加
//descendantFocusability屬性的作用是當(dāng)一個(gè)view獲取焦點(diǎn)時(shí)仑撞,定義viewGroup和其子控件兩者之間的關(guān)系。而blocksDescendants是viewgroup會(huì)覆蓋子類控件而直接獲得焦點(diǎn)妖滔。
android:descendantFocusability="blocksDescendants"
十九隧哮、禁止recyclerview滑動(dòng)
LinearLayoutManager manager = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
二十、SDKManager配置
二十一座舍、解決Scrollview在滑動(dòng)時(shí)的點(diǎn)擊回到頂部
mScroll.scrollTo(0, 0);
mScroll.smoothScrollTo(0, 0);
二十二沮翔、解決App在第一次啟動(dòng)時(shí)白屏?xí)r間過(guò)長(zhǎng)。
由于AndroidStudio2.0之后多了Instant Run功能曲秉,該功能用來(lái)提高開(kāi)發(fā)效率采蚀,然而為了能讓Instant Run可以正常工作,App在首次啟動(dòng)時(shí)會(huì)初始化相關(guān)工作承二,從而出現(xiàn)白屏現(xiàn)象榆鼠。但是用戶并不理解這些,只會(huì)讓用戶體驗(yàn)不好亥鸠。
解決方法:將App打包成release包妆够,就會(huì)解決白屏現(xiàn)象。
進(jìn)一步優(yōu)化
Theme中
// 可以讓程序在初始化時(shí)是透明的
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
二十三负蚊、Android5.0以上 webView嵌套Https視頻無(wú)法播放
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}