问题
I'm trying to create a monitor service which would know when apps start/resume/restart/pause/... and also when apps crashes.
I have tried to read logcat, but it seems that calls to [onCreate()/onStart()/onRestart()/...] are not logged into logcat.
Is there a way to catch all these method calls for all running apps ? Is there a way to know when an app crash ?
Note: The service don't need to be notified as soon as these events happens, few seconds/minutes after is also okay.
回答1:
Refer to my code in this answer:
Android how to know an app has been started and range apps priority according the starting times
You can monitor it as follows:
If an app is in the foreground, that is if it is in start, then it will be the first item of the List taskInfo.
If it is paused, it will no longer be the first item in that list.
To check resume, see if it previously was not the first item, but , in the current iteration, it is the first item.
I am not too sure about the crash part, but maybe you can find a similar way from the above logic.
回答2:
You can check out Crashlytics for crash tracking. You can see crashed lines in methods as well.
来源:https://stackoverflow.com/questions/20882743/how-to-monitor-all-running-apps-activities-lifecycles-and-crashes