(ii) monitor user behavior and app online status and online duration only through application

Source: Internet
Author: User

First to implement the function, or first from the API to find. See if there's anything you want. This is actually listening to the status of activity within the app. What to do?

To an API location: http://www.android-doc.com/reference/android/app/Application.html

  

Did you see that? Here's a way to registeractivitylifecyclecallbacks , isn't that listening? Look at the callback, go in and see.

I'll go, this service is not too good. What do you want? Did you see that? The life cycle of all activity can be obtained. So. How to use it has become a matter of consideration.

Here's what I do statistics active and the duration of the

Importandroid.app.Activity;Importandroid.app.Application;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by admin on 2018/3/2. *@authorCxx * Note: The user's actions are judged based on the activity's life cycle. End the app timing statistics in both cases. * One is: When the activity on top or top of the stack is suspended, it is time to force the process to be killed. * Two types are: when the user returns to the bottom level of activity, and finish. That is: Empty the activity stack end app*/ Public classTimerecord {Private BooleanIsappalive =true;//determine if the app timer is over, and if it ends, but the app hasn't been killed, use this field to revive the timekeeping function    Private BooleanIsswitchactivity =false; Private BooleanIsappexit =false; PrivateString topactivity; PrivateMetaclass Helper; Privatemap<string,string> map =NewHashmap<>(); Private LongTimestart = 0;//time stamp to start timing    PrivateString TAG = "Cxx";  Public voidInitFinalContext Context,metaclass Metaclass) {Application Application=(application) Context.getapplicationcontext (); //Escalate Active VolumeSystem.out.println (tag+ "escalation activity-Initialize");  This. Helper =Metaclass;        Helper.reportactive (); Timestart= System.currenttimemillis ()/1000; System.out.println (TAG+ "Start Timer-Initialize" +Timestart); Application.registeractivitylifecyclecallbacks (Newapplication.activitylifecyclecallbacks () {@Override Public voidonactivitycreated (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 voidonactivitystarted (activity activity) {SYSTEM.OUT.PRINTLN (TAG+ "Started_activity:" +Activity.getclass (). Getsimplename ()); } @Override Public voidonactivityresumed (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+ "Escalation of active volume--onresumed");                    Helper.reportactive (); Timestart= System.currenttimemillis ()/1000; System.out.println (TAG+ "Start Chronograph--onresumed" +Timestart); Isappalive=true; }} @Override Public voidonactivitypaused (activity activity) {} @Override Public voidonactivitystopped (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) {                        LongTimeend = System.currenttimemillis ()/1000; if(helper!=NULL){                            LongTimegap = timeend-Timestart; String Onlinetime=string.valueof (TIMEGAP); System.out.println (TAG+ "timed end--stopped:" +timeend); System.out.println (TAG+ "Timing End length--stopped:" +onlinetime);                        Helper.reportonline (Onlinetime); } isappalive=false; } }} @Override Public voidonactivitysaveinstancestate (activity activity, bundle bundle) {} @Override Public voidonactivitydestroyed (activity activity) {Map.Remove (Activity.getclass (). Getsimplename ()); if(Map.size () ==0&&isappalive) {                    LongTimeend = System.currenttimemillis ()/1000; if(helper!=NULL){                        LongTimegap = timeend-Timestart; String Onlinetime=string.valueof (TIMEGAP); System.out.println (TAG+ "timed end--destory:" +timeend); System.out.println (TAG+ "Timing End length--destory:" +onlinetime);                    Helper.reportonline (Onlinetime); } isappalive=false; }                if(Map.size () ==0) {Isappexit=true;    }            }        }); }}

(ii) monitor user behavior and app online status and online duration only through application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.