尝试修复安装完成触发的 ANR

This commit is contained in:
juntao
2020-12-14 17:10:55 +08:00
parent 5246d6e743
commit 3fbcd33f98
3 changed files with 17 additions and 2 deletions

View File

@ -185,6 +185,11 @@ public class PackageUtils {
public static boolean compareSignatureBetweenInstalledAppWithApk(Context context, String packageName, String apkFilePath) {
try {
// 统计签名比较使用的频率 (大文件会触发 ANR)
SentryHelper.INSTANCE.onEvent(
"SIGNATURE_COMPARE",
"packageName", packageName);
Signature sig = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures[0];
// Fuck HUAWEI, 华为系统调用 getPackageArchiveInfo 获取魔羯 apk 的签名时会耗时超过5秒造成 ANR没有找到解决方法

View File

@ -7,6 +7,7 @@ import android.preference.PreferenceManager
import android.text.TextUtils
import com.gh.common.constant.Constants
import com.gh.common.loghub.LoghubUtils
import com.gh.common.runOnIoThread
import com.gh.common.util.*
import com.gh.gamecenter.entity.GameDigestEntity
import com.gh.gamecenter.eventbus.EBPackage
@ -106,7 +107,8 @@ object PackageObserver {
}
})
}
postNewlyInstalledApp(gameId, packageName)
runOnIoThread { postNewlyInstalledApp(gameId, packageName) }
}
if ("卸载" == busFour.type) {

View File

@ -4,6 +4,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.gh.common.AppExecutor;
import com.gh.common.util.DataUtils;
import com.gh.common.util.ExtensionsKt;
import com.gh.common.util.InstallUtils;
@ -38,7 +39,14 @@ public class InstallAndUninstallReceiver extends BroadcastReceiver {
String versionName = PackageUtils.getVersionByPackage(packageName);
EBPackage installEb = new EBPackage("安装", packageName, versionName);
EventBus.getDefault().post(installEb);
PackageObserver.onPackageChanged(installEb);
if (PackageUtils.isAppOnForeground(context)) {
PackageObserver.onPackageChanged(installEb);
} else {
// 处于后台运行的时候尝试延迟 1 秒再触发 onPackageChanged (猜测是引起 ANR 的原因)
AppExecutor.getUiExecutor().executeWithDelay(() -> {
PackageObserver.onPackageChanged(installEb);
}, 1000);
}
}
// 接收卸载广播