添加启动安装或卸载时5分钟的监听(扫描本地包,检查指定包是否存在或不存在)
This commit is contained in:
148
app/src/main/java/com/gh/common/util/InstallUtils.java
Normal file
148
app/src/main/java/com/gh/common/util/InstallUtils.java
Normal file
@ -0,0 +1,148 @@
|
||||
package com.gh.common.util;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
|
||||
import com.gh.gamecenter.eventbus.EBPackage;
|
||||
import com.gh.gamecenter.manager.CommentManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
/**
|
||||
*
|
||||
* 下载完成跳安装,
|
||||
*/
|
||||
public class InstallUtils {
|
||||
|
||||
private static final int MAX_TIME = 5 * 60 * 1000;
|
||||
|
||||
private static Map<String, Long> installMap;
|
||||
private static Map<String, Long> uninstallMap;
|
||||
|
||||
private static InstallUtils mInstance;
|
||||
|
||||
public static InstallUtils getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
synchronized (CommentManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new InstallUtils(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
private PackageManager packageManager;
|
||||
|
||||
private Handler handler;
|
||||
|
||||
private boolean isRunning;
|
||||
|
||||
private InstallUtils(Context context) {
|
||||
isRunning = false;
|
||||
|
||||
packageManager = context.getPackageManager();
|
||||
|
||||
handler = new Handler(context.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == 0x123 && packageManager != null) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
list.add(packageInfo.packageName);
|
||||
}
|
||||
}
|
||||
if (installMap != null && installMap.size() != 0) {
|
||||
ArrayList<String> keys = new ArrayList<>();
|
||||
for (String packageName : installMap.keySet()) {
|
||||
long time = installMap.get(packageName);
|
||||
if (System.currentTimeMillis() - time >= MAX_TIME) {
|
||||
keys.add(packageName);
|
||||
} else if (list.contains(packageName)) {
|
||||
keys.add(packageName);
|
||||
EventBus.getDefault().post(new EBPackage("安装", packageName));
|
||||
}
|
||||
}
|
||||
for (String key : keys) {
|
||||
installMap.remove(key);
|
||||
}
|
||||
}
|
||||
if (uninstallMap != null && uninstallMap.size() != 0) {
|
||||
ArrayList<String> keys = new ArrayList<>();
|
||||
for (String packageName : uninstallMap.keySet()) {
|
||||
long time = uninstallMap.get(packageName);
|
||||
if (System.currentTimeMillis() - time >= MAX_TIME) {
|
||||
keys.add(packageName);
|
||||
} else if (list.contains(packageName)) {
|
||||
keys.add(packageName);
|
||||
EventBus.getDefault().post(new EBPackage("卸载", packageName));
|
||||
}
|
||||
}
|
||||
for (String key : keys) {
|
||||
uninstallMap.remove(key);
|
||||
}
|
||||
}
|
||||
if ((installMap != null && installMap.size() != 0)
|
||||
|| (uninstallMap != null && uninstallMap.size() != 0)) {
|
||||
sendEmptyMessageDelayed(0x123, 3000);
|
||||
} else {
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void addInstall(String packageName) {
|
||||
if (installMap == null) {
|
||||
installMap = Collections.synchronizedMap(new HashMap<String, Long>());
|
||||
}
|
||||
installMap.put(packageName, System.currentTimeMillis());
|
||||
run();
|
||||
}
|
||||
|
||||
public void removeInstall(String packageName) {
|
||||
if (installMap != null) {
|
||||
installMap.remove(packageName);
|
||||
}
|
||||
}
|
||||
|
||||
public void addUninstall(String packageName) {
|
||||
if (uninstallMap == null) {
|
||||
uninstallMap = Collections.synchronizedMap(new HashMap<String, Long>());
|
||||
}
|
||||
uninstallMap.put(packageName, System.currentTimeMillis());
|
||||
run();
|
||||
}
|
||||
|
||||
public void removeUninstall(String packageName) {
|
||||
if (uninstallMap != null) {
|
||||
uninstallMap.remove(packageName);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if ((installMap == null || installMap.isEmpty()) && (uninstallMap == null || uninstallMap.isEmpty())) {
|
||||
return;
|
||||
}
|
||||
if (isRunning) {
|
||||
return;
|
||||
}
|
||||
isRunning = true;
|
||||
handler.sendEmptyMessageDelayed(0x123, 10000);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user