環(huán)信即時(shí)通訊
Q:找不到AsyncTaskCompatl類 NoClassDefFoundError: support/v4/os/AsyncTaskCompat
A:因?yàn)锳syncTaskCompat此類已在API26.0.0以上中被棄用了秩铆,所以需要更改compileSdkVersion最高26,同理targetSdkVersion和其它依賴也需要適當(dāng)降低版本滚粟。
Service里啟動(dòng)Activity延時(shí)問題
Q:Service里監(jiān)聽按鍵然后啟動(dòng)Activity凡壤,大約延時(shí)2-3s左右才啟動(dòng)
A:使用延時(shí)意圖
intent intent = new Intent(context, AnotherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
getSupportFragmentManager()報(bào)紅
app包下FragmentManager用:
Fragmentmanager fragmentManager=getFragmentManager();
v-4包的FragmentManager用:
FragmentManager fragmentManager=getSupportFragmentManager() 獲取
但是getSupportFragmentManager() 有其運(yùn)用范圍俗扇,只能在部分activity中運(yùn)用铜幽。當(dāng)遇到getSupportFragmentManager() 沒定義的問題時(shí)狮杨,修改下activity為FragmentActivity或者AppCompatActivity橄教。
旋轉(zhuǎn)動(dòng)畫的時(shí)候會(huì)有停頓
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:fromDegrees="0"
android:toDegrees="359" />
</set>
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
//解決旋轉(zhuǎn)有停頓
LinearInterpolator lir = new LinearInterpolator();
rotateAnimation.setInterpolator(lir);
mIvLoading.startAnimation(rotateAnimation);
Error: Program type already present: android.support.v4.app.INotificationSideChannel
當(dāng)項(xiàng)目使用API28對應(yīng)的是android9.0,當(dāng)AndroidStudio運(yùn)行項(xiàng)目的時(shí)候出現(xiàn)編譯錯(cuò)誤:Error: Program type already present: android.support.v4.app.INotificationSideChannel,這意思是v4包沖突了比吭。
首先,查找一下自己的文件里是否存在v4包重復(fù)的赏表,搜索之后發(fā)現(xiàn)沒有出現(xiàn)重復(fù),針對這個(gè)問題查看了網(wǎng)上一些資料间狂,發(fā)現(xiàn)這個(gè)問題是AndroidX版本引起的。有以下兩種解決方法:
1纺弊、Refactor-->Migrate to AndroidX
2、向gradle.properties添加以下內(nèi)容:
android.enableJetifier=true
android.useAndroidX=true
Android與AndroidX包重復(fù)問題詳情可查看這篇文章:http://www.reibang.com/p/1466ebefe4d0
如果SDK不是28的版本焰望,可在Terminal窗口復(fù)制命令gradlew clean build --stacktrace去查看詳細(xì)信息針對性去解決問題~
zip4j jar包使用中遇到解壓文件名字部分出現(xiàn)亂碼
ZipFile zipFile = new ZipFile(zipfilenow.getAbsolutePath());
zipFile.setFileNameCharset(getCharset(zipfilenow.getPath()));
/**
* 判斷文件的編碼格式
*
* @param fileName
* @return
*/
public static String getCharset(String fileName) {
BufferedInputStream bis = null;
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
bis = new BufferedInputStream(new FileInputStream(fileName));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
}
bis.mark(0);
if (!checked) {
while ((read = bis.read()) != -1) {
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 單獨(dú)出現(xiàn)BF以下的捆姜,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 雙字節(jié) (0xC0 - 0xDF)
// (0x80 - 0xBF),也可能在GB編碼內(nèi)
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出錯(cuò),但是幾率較小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return charset;
}