本文共 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/