先要实现功能,还是先从API去找。看看有没有你想要的。这里其实就是监听App内activity的状态。怎么办?
给个API所在地址:http://www.android-doc.com/reference/android/app/Application.html
看到没?这里有个方法registerActivityLifecycleCallbacks ,这不是监听吗?看看回调了啥东西,点进去再看看。
我去,这服务不要太好呀。要啥有啥。看到没?所有activity的生命周期都能获取到。所以。怎么用就成了改考虑的问题了。
下面是我做的统计活跃量和时长的
import android.app.Activity; import android.app.Application; import android.content.Context; import android.os.Bundle; import android.util.Log; import java.util.HashMap; import java.util.Map; /** * Created by admin on 2018/3/2. * @author cxx * note: 根据activity的生命周期来判断用户的操作。在两种情况下结束app计时统计。 * 一种是:当处于栈顶或顶层的activity被挂起,要强制杀掉进程的时候。 * 二种是:当用户逐层退回到最底层的activity,并finish的时候。即:清空activity栈结束app */ public class TimeRecord { private boolean isAppAlive = true; //判断app计时是否已经结束,如果结束,但是app并没有被杀掉,用此字段复活计时功能 private boolean isSwitchActivity = false; private boolean isAppExit = false; private String topActivity ; private MetaClass helper ; private Map<String ,String> map = new HashMap<>(); private long timeStart =0; //开始计时的时间戳 private String TAG = "cxx"; public void init(final Context context,MetaClass metaClass){ Application application = (Application)context.getApplicationContext(); //上报活跃量 System.out.println(TAG+"上报活跃量——初始化"); this.helper = metaClass; helper.reportActive(); timeStart = System.currentTimeMillis()/1000; System.out.println(TAG+"开始计时——初始化"+timeStart); application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle bundle) { topActivity = activity.getClass().getSimpleName(); map.put(topActivity,topActivity); isAppAlive = true; isSwitchActivity = false; System.out.println(TAG+"Created_activity:"+activity.getClass().getSimpleName()); } @Override public void onActivityStarted(Activity activity) { System.out.println(TAG+"Started_activity:"+activity.getClass().getSimpleName()); } @Override public void onActivityResumed(Activity activity) { System.out.println(TAG+"Resumed_activity:"+activity.getClass().getSimpleName()); if(!activity.getClass().getSimpleName().equals(topActivity)){ isSwitchActivity = true; }else{ isSwitchActivity = false; } topActivity=activity.getClass().getSimpleName(); if(!isAppAlive||isAppExit){ isAppExit = false; System.out.println(TAG+"上报活跃量——onResumed"); helper.reportActive(); timeStart = System.currentTimeMillis()/1000; System.out.println(TAG+"开始计时——onResumed"+timeStart); isAppAlive = true; } } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { System.out.println(TAG+"stop_activity:"+activity.getClass().getSimpleName()); System.out.println(TAG+"stop_topActivity:"+topActivity); if(topActivity.equals(activity.getClass().getSimpleName())){ if(!isSwitchActivity){ long timeEnd = System.currentTimeMillis()/1000; if(helper!=null){ long timegap = timeEnd-timeStart; String onlineTime = String.valueOf(timegap); System.out.println(TAG+"计时结束——stopped:"+timeEnd); System.out.println(TAG+"计时结束时长——stopped:"+onlineTime); helper.reportOnline(onlineTime); } isAppAlive=false; } } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { } @Override public void onActivityDestroyed(Activity activity) { map.remove(activity.getClass().getSimpleName()); if(map.size()==0&&isAppAlive){ long timeEnd = System.currentTimeMillis()/1000; if(helper!=null){ long timegap = timeEnd-timeStart; String onlineTime = String.valueOf(timegap); System.out.println(TAG+"计时结束——destory:"+timeEnd); System.out.println(TAG+"计时结束时长——destory:"+onlineTime); helper.reportOnline(onlineTime); } isAppAlive = false; } if(map.size() ==0){ isAppExit = true; } } }); } }
原文地址:https://www.cnblogs.com/shoneworn/p/8527650.html
时间: 2024-09-27 23:21:46