2
This commit is contained in:
@@ -309,14 +309,17 @@ public class BaseApplication extends Application {
|
|||||||
// CrashHandler.getInstance().init(getApplicationContext());
|
// CrashHandler.getInstance().init(getApplicationContext());
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// 注册Activity生命周期回调,用于跟踪当前resumed的Activity
|
// 注册Activity生命周期回调,用于跟踪当前resumed的Activity并在退后台时上报缓存画像埋点
|
||||||
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
|
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
|
||||||
|
private int startedActivityCount = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityStarted(Activity activity) {
|
public void onActivityStarted(Activity activity) {
|
||||||
|
startedActivityCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -334,6 +337,10 @@ public class BaseApplication extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityStopped(Activity activity) {
|
public void onActivityStopped(Activity activity) {
|
||||||
|
startedActivityCount = Math.max(0, startedActivityCount - 1);
|
||||||
|
if (startedActivityCount == 0 && activity != null && !activity.isChangingConfigurations()) {
|
||||||
|
startReport(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1531,6 +1531,11 @@ public abstract class BaseActivity extends AppCompatActivity implements
|
|||||||
if (type==2){
|
if (type==2){
|
||||||
type =4;
|
type =4;
|
||||||
}
|
}
|
||||||
|
if (type == 4 && context != null) {
|
||||||
|
Intent reportIntent = new Intent(context, ReportService.class);
|
||||||
|
reportIntent.putExtra("report_token", Constant.Repotr_token);
|
||||||
|
context.startService(reportIntent);
|
||||||
|
}
|
||||||
String uuid = SharePrefUtil.getString(context, Constant.uuid, "");
|
String uuid = SharePrefUtil.getString(context, Constant.uuid, "");
|
||||||
RequestParams params = new RequestParams();//封装参数
|
RequestParams params = new RequestParams();//封装参数
|
||||||
params.addBodyParameter("expertUuid", uuid);
|
params.addBodyParameter("expertUuid", uuid);
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ public class ReportService extends Service {
|
|||||||
}
|
}
|
||||||
private static final ExecutorService REPORT_EXECUTOR = Executors.newSingleThreadExecutor();
|
private static final ExecutorService REPORT_EXECUTOR = Executors.newSingleThreadExecutor();
|
||||||
private static final List<reportBean> PENDING_BEFORE_TOKEN_LIST = new ArrayList<>();
|
private static final List<reportBean> PENDING_BEFORE_TOKEN_LIST = new ArrayList<>();
|
||||||
|
/** 普通浏览埋点攒批阈值(满 20 条自动打包上传) */
|
||||||
|
private static final int BATCH_UPLOAD_THRESHOLD = 20;
|
||||||
|
|
||||||
public String ReportFile;
|
public String ReportFile;
|
||||||
public String ReportUploadingDir;
|
public String ReportUploadingDir;
|
||||||
@@ -155,7 +157,7 @@ public class ReportService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Token 已就绪:先把之前暂存的埋点补齐 user_id/app_key 并落盘
|
// Token 已就绪:先把之前暂存的埋点补齐 user_id/app_key 并落盘
|
||||||
flushPendingBeforeTokenList();
|
boolean pendingHasImmediate = flushPendingBeforeTokenList();
|
||||||
|
|
||||||
if (incomingList == null) {
|
if (incomingList == null) {
|
||||||
if (checkReport()) {
|
if (checkReport()) {
|
||||||
@@ -165,25 +167,63 @@ public class ReportService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!incomingList.isEmpty()) {
|
if (!incomingList.isEmpty()) {
|
||||||
writeReport(incomingList);
|
writeReport(incomingList, pendingHasImmediate);
|
||||||
|
} else if (pendingHasImmediate && checkReport()) {
|
||||||
|
zipFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void flushPendingBeforeTokenList() {
|
private boolean flushPendingBeforeTokenList() {
|
||||||
if (!PENDING_BEFORE_TOKEN_LIST.isEmpty()) {
|
if (!PENDING_BEFORE_TOKEN_LIST.isEmpty()) {
|
||||||
|
boolean hasImmediate = shouldFlushImmediately(PENDING_BEFORE_TOKEN_LIST);
|
||||||
CommUtil.saveListToFile(PENDING_BEFORE_TOKEN_LIST, ReportFile, "upload", ReportFileZip);
|
CommUtil.saveListToFile(PENDING_BEFORE_TOKEN_LIST, ReportFile, "upload", ReportFileZip);
|
||||||
CommUtil.totalreport += PENDING_BEFORE_TOKEN_LIST.size();
|
CommUtil.totalreport += PENDING_BEFORE_TOKEN_LIST.size();
|
||||||
PENDING_BEFORE_TOKEN_LIST.clear();
|
PENDING_BEFORE_TOKEN_LIST.clear();
|
||||||
|
return hasImmediate;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是否包含高优先级关键业务事件(登录、签到、支付/积分兑换、文献下载、修改资料、收藏、分享等),
|
||||||
|
* 关键事件落盘后立即触发打包上传,保证实时性。
|
||||||
|
*/
|
||||||
|
private boolean shouldFlushImmediately(List<reportBean> beanList) {
|
||||||
|
if (beanList == null || beanList.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (reportBean item : beanList) {
|
||||||
|
if (item == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!StringUtil.isEmpty(item.getEvent_val())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
String eventName = item.getEvent_nickname();
|
||||||
|
if ("login".equals(eventName)
|
||||||
|
|| "signin".equals(eventName)
|
||||||
|
|| "magazine_donwload".equals(eventName)
|
||||||
|
|| "modify".equals(eventName)
|
||||||
|
|| "collect".equals(eventName)
|
||||||
|
|| "share".equals(eventName)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeReport(List<reportBean> currentList) {
|
public void writeReport(List<reportBean> currentList) {
|
||||||
// 始终先把当前批次写入本地 upload.txt,避免第 100 条埋点丢失或上传期间新埋点丢失
|
writeReport(currentList, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeReport(List<reportBean> currentList, boolean forceImmediate) {
|
||||||
|
// 始终先把当前批次写入本地 upload.txt,避免触发阈值那条埋点丢失或上传期间新埋点丢失
|
||||||
CommUtil.saveListToFile(currentList, ReportFile, "upload", ReportFileZip);
|
CommUtil.saveListToFile(currentList, ReportFile, "upload", ReportFileZip);
|
||||||
CommUtil.totalreport += currentList.size();
|
CommUtil.totalreport += currentList.size();
|
||||||
Log.d("okhttp", "CommUtil.totalreport " + CommUtil.totalreport);
|
Log.d("okhttp", "CommUtil.totalreport " + CommUtil.totalreport);
|
||||||
|
|
||||||
if (CommUtil.totalreport >= 100 && checkReport()) {
|
boolean immediate = forceImmediate || shouldFlushImmediately(currentList);
|
||||||
|
if ((immediate || CommUtil.totalreport >= BATCH_UPLOAD_THRESHOLD) && checkReport()) {
|
||||||
zipFile();
|
zipFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,7 +292,7 @@ public class ReportService extends Service {
|
|||||||
// 仅清理已上传成功的快照目录 ReportUploadingDir,保留上传期间新写入 ReportFile 的数据
|
// 仅清理已上传成功的快照目录 ReportUploadingDir,保留上传期间新写入 ReportFile 的数据
|
||||||
CommUtil.deleteFile(ReportFileZip, "outputandroid.zip", ReportUploadingDir);
|
CommUtil.deleteFile(ReportFileZip, "outputandroid.zip", ReportUploadingDir);
|
||||||
CommUtil.isReoprtingNow = "false";
|
CommUtil.isReoprtingNow = "false";
|
||||||
if (CommUtil.totalreport >= 100 && checkReport()) {
|
if (CommUtil.totalreport >= BATCH_UPLOAD_THRESHOLD && checkReport()) {
|
||||||
zipFile();
|
zipFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -370,8 +410,8 @@ public class ReportService extends Service {
|
|||||||
REPORT_EXECUTOR.execute(new Runnable() {
|
REPORT_EXECUTOR.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
flushPendingBeforeTokenList();
|
boolean hasImmediate = flushPendingBeforeTokenList();
|
||||||
if (!retryUploadAfterToken && CommUtil.totalreport >= 100 && checkReport()) {
|
if (!retryUploadAfterToken && (hasImmediate || CommUtil.totalreport >= BATCH_UPLOAD_THRESHOLD) && checkReport()) {
|
||||||
zipFile();
|
zipFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user