博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android应用中自动化埋点的实现,Android 自动化埋点方案
阅读量:5254 次
发布时间:2019-06-14

本文共 5096 字,大约阅读时间需要 16 分钟。

一、事件实现原理:

① View设置AccessibilityDelegate

②而当View 产生了click,long_click 等事件的时候.会在响应原有的Listener方法

③原有的Listener方法响应结束之后,然后在sendAccessibilityEvent方法中转发消息到AccessibilityDelegate

④在AccessibilityDelegate的sendAccessibilityEvent处理该事件

⑤不要调用call开头的方法如callOnClick,因为不能发送事件到sendAccessibilityEvent

如下方法可以使用

public boolean performClick() {

final boolean result;

final ListenerInfo li = mListenerInfo;

if (li != null && li.mOnClickListener != null) {

playSoundEffect(SoundEffectConstants.CLICK);

li.mOnClickListener.onClick(this);

result = true;

} else {

result = false;

}

sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);

notifyEnterOrExitForAutoFillIfNeeded(true);

return result;

}

⑥、sendAccessibilityEvent生成事件的key和value

public void sendAccessibilityEvent(View host, int eventType) {

host.sendAccessibilityEventInternal(eventType);

}

二、Android组件生命周期埋点

①Activity生命周期

我们可以通过Application.ActivityLifecycleCallbacks来实现监听Activity的活动

②Service & BroadcastReciever生命周期

目前只能通过手动方式,至于实现方式可以修改如下代码

public class ActivityStatusManager implements Application.ActivityLifecycleCallbacks{

public final Integer STATUS_CREATED = 0x0001;

public final Integer STATUS_RESUMED = 0x0002;

public final Integer STATUS_PAUSED = 0x0003;

public final Integer STATUS_STARTED = 0x0004;

public final Integer STATUS_STOPED = 0x0005;

public final Integer STATUS_DESTROYED = -1;

private Map activityStack;

public void registerActivityLifecycleCallbacks(Application application) {

application.registerActivityLifecycleCallbacks(this);

}

public void unregisterActivityLifecycleCallbacks(Application application){

application.unregisterActivityLifecycleCallbacks(this);

}

private static class Holder{

public final static ActivityStatusManager instance = new ActivityStatusManager();

}

public static ActivityStatusManager shareInstance(){

return Holder.instance;

}

private ActivityStatusManager(){

activityStack = new ArrayMap();

}

public Integer getActivityStatus(Activity activity) {

final Integer status = activityStack.get(activity);

if(status==null){

return STATUS_DESTROYED;

}

return status;

}

@Override

public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

if(activity!=null){

activityStack.put(activity,STATUS_CREATED);

}

}

@Override

public void onActivityStarted(Activity activity) {

if(activity!=null){

activityStack.put(activity,STATUS_STARTED);

}

}

@Override

public void onActivityResumed(Activity activity) {

if(activity!=null){

activityStack.put(activity,STATUS_RESUMED);

}

}

@Override

public void onActivityPaused(Activity activity) {

if(activity!=null){

activityStack.put(activity,STATUS_PAUSED);

}

}

@Override

public void onActivityStopped(Activity activity) {

if(activity!=null){

activityStack.put(activity,STATUS_STOPED);

}

synchronized (Holder.instance){

int counter = 0;

for (Map.Entry entry : activityStack.entrySet()){

if(entry.getValue()==STATUS_STOPED){

counter++;

}

}

if(counter>0 && activityStack.size()==counter){

BaseApplication.setInForeground(false);

}else{

BaseApplication.setInForeground(true);

}

}

}

@Override

public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}

@Override

public void onActivityDestroyed(Activity activity) {

Application application = activity.getApplication();

activityStack.remove(activity);

clearInvalidActivity();

if(activityStack.isEmpty() && application!=null){

unregisterActivityLifecycleCallbacks(application);

}

}

public void clearInvalidActivity(){

Iterator> iterator = activityStack.entrySet().iterator();

do{

if(!iterator.hasNext()) return;

Map.Entry entry = iterator.next();

if(entry!=null && activityIsDestroyed(entry.getKey()) ){

iterator.remove();

}

}while(iterator.hasNext());

}

public void finishAll(){

if(activityStack.isEmpty()) return;

Activity activity = null;

Iterator> iterator = activityStack.entrySet().iterator();

do{

if(!iterator.hasNext()) return;

Map.Entry entry = iterator.next();

if(entry!=null ){

activity = entry.getKey();

if(activity!=null && !activityIsDestroyed(activity))

{

activity.finish();

}

iterator.remove();

}

}while(iterator.hasNext());

}

/**

* 程序是否在前台运行

*

* @return

*/

public boolean isForegroundProcess(Context context) {

// Returns a list of application processes that are running on the

// device

ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);

String packageName = context.getApplicationContext().getPackageName();

List appProcesses = activityManager

.getRunningAppProcesses();

if (appProcesses == null)

return false;

for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {

// The name of the process that this object is associated with.

if (appProcess.processName.equals(packageName)

&& appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {

return true;

}

}

return false;

}

private boolean activityIsDestroyed(Activity thatActivity){

if(thatActivity==null) return true;

if(Build.VERSION.SDK_INT>=17){

return thatActivity.isDestroyed() || thatActivity.isFinishing();

}else{

return thatActivity.isFinishing();

}

}

}

三、配置环境埋点

在Application中注册 ComponentCallbacks2

转载地址:http://ozrav.baihongyu.com/

你可能感兴趣的文章
hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
查看>>
hdu 5769 Substring 后缀数组 + KMP
查看>>
springmvc代码执行流程
查看>>
Spring Boot学习(四)
查看>>
Java NIO与IO的详细区别(通俗篇)
查看>>
The Reflection And Amplification Attacks && NTP Reply Flood Attack Based On NTP
查看>>
Web后台快速开发框架
查看>>
关于zipfile解压出现的字符编码问题
查看>>
Windows API 弹出文本框输入的内容
查看>>
UVALive - 6571 It Can Be Arranged 最大流
查看>>
Javascript学习笔记(二)在HTML中使用Javascript
查看>>
完全背包
查看>>
Cookie、 LocalStorage 与 SessionStorage详解
查看>>
Thuwc2018 游记
查看>>
R语言输入与输出
查看>>
国土档案管理信息系统【档案著录】-他项权利类档案著录
查看>>
P3366 【模板】最小生成树
查看>>
一个屌丝程序猿的人生(六十九)
查看>>
(二)代理模式详解(包含原理详解)
查看>>
webgame(php+flex) 的优化方案。
查看>>