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.
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.
You can check out Crashlytics for crash tracking. You can see crashed lines in methods as well.
Related
H, I miss a lot the old time when users could comment on crashes in Android.
Now I realise that the best way to reproduce something like this would be if the app could detect when it was open for the first time right after a Fatal error, so I/the app can advise the user to send a personalised comment on how the crash happened.
Is there a way, using Firebase/Crashlytics, to know that the app crashed previously, when opening an app?
If there is an uncaught exception in an activity (or service) the activity is forcably closed and the previos activity of the app is shown. If the last Activity causes the crash the app is closed.
What you can do to detect this situation is to register a global-uncaught-exception-handler that writes `firebaseException=true" to a file/app-settings if the exception comes from firebase before calling the previous global-uncaught-exception-handler.
In every Activity-s onCreate you can load firebaseException and act according (do not forget to write firebaseException=false after.
I have never worked with Crashlytics but i assume that it also hooks into uncaught-exception-handler so it is important that Crashlytics is initialized before you set your own handler and that your handler calls the previous uncaught-exception-handler when done
You may use custom Crashlytics webhooks to notify your service about crashes and reach back next time is opened.
I'm building an app that sometimes crashes, I want to know that it crashed in the next time I opened it so I can suggest to the user some post-crash options.
How can I detect the crash?
Also I want to be able to save the user's work before it crashes, I means real time detection of crash, can I do it without knowing where it crashed?
You will need to know where it crashed in order to set the try/catch blocks in the right place to, er, catch the crash and save the data or whatever you have in mind.
This is known as "graceful" termination if you want to consider it in more detail.
Unfortunately neither Java destructor/finalize methods nor lifecycle methods such as onDestroy are anywhere near as robust as try/catch blocks so I'm afraid that is your only option, and how many of us deal with exception prevention. No-one would wittingly provide a user experience that crashes, much less with loss of their data.
Take a took at the ACRA library. You can configure it so whenever a crash happens you can control it and even send the crash log by email
You can use try/catch blocks, then send details on the Exception in your catch.
There are implement UncaughtExceptionHandler as mentioned in these answers and write crash report in some file or use it another way.
ACRA is already mentioned.
However for paid version, I found BugSnag is very good at this.
Or if you want to take the extra mile, try AppSee.
AppSee even has video recording session of how the crash happens. It is from tapping that button on the second list, the menu button or even when the user slides left in your viewpager.
in my app I am using push notifications to notify the user about something.Based on different app state I have to assign different activities to click event of notification. If the user press home button and app is in background i have to handle that scenario as well. But here the issue come when user clear the app instance from memory. If user do that I have to consider it as app closed. But i dont know how to get that app instance clear event. Please help me.
The task you are willing to solve is equivalent to protection against the task manager. I am not sure that your app knows better than the user whether or not to show the ads. Please do not create malware.
Try the UncaughtExceptionHandler stuff like setUncaughtExceptionHandler(). I am not sure it will be necessarily called, and it may depend on the Android version, but it is something to start with.
Another possibility is to use one or more remote service(s). The processes will not die all at once, there's a good chance that one process may notice the death of another process. There should be some kind of wait-for-process-to-complete at least on the JNI level.
One more possibility is to use two applications, one monitoring the other...
After you solve this problem, please post your own answer telling the world what you did.
I want to know the average start-up time of all the apps on android device. And this is supposed to be done by my app. So, is it possible to get such information from the app level programatically?
I assume you mean the time it takes for a given app to complete its onCreate() method. You might be able to do something on a rooted phone with extending onCreate() with a timer that logs to somewhere for your app.
This isn't going to work. You would have to start an app and time it, but you do not have access to the information about when another app has finished loading. Furthermore,.. how do you define start-up time? The time taken until the app opens or the time until past the loading screen.. or the time until past the splash screen...?
The way I am trying to solve this problem is by using a service to monitor the logs that will start a timer when app X is started. We have that working properly. The problem we are having is how to define the app as done loading. Once we have that we can gather data. But I am still looking for a way to know when app X moves from the onCreate() method from one activity to the onCreate() method of another. This will obviously only work for apps that have a loading activity for their app.
Effectively, this a really hard problem to both define and solve. If you ever come up with anything different than what I have, I'd love to hear it.
I have a very common problem but wasn't able to find a proper solution/pattern to solve it. My application has two kinds of data that need to be automatically updated:
general data
user-related data
If the user is not logged in, only general data are downloaded and displayed, if the user is logged in, his/her data are handled too.
I need to periodically download these data from a Web service, starting from the boot of the device (after the action android.intent.action.BOOT_COMPLETED is triggered).
Since my app can be moved to the SD, I will also need to register for the android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE action.
Issue 1: starting from API level 11 (Honeycomb) all actions aren't sent to applications in a stopped state. I haven't quite understood this, does this mean that my app can't listen for actions if it has just been installed and never opened (so only once if we count updates out)? Or does this mean that, after every system reboot, the action will be triggered only when the application starts?
Issue 2: if the application has been moved to SD, SyncAdapters won't be able to be run, so I have to rely on the general BroadcastReceiver-Service-Alarm-PendingIntent strategy. But how can I understand if the SyncAdapter won't be started by the system? (I already handle Accounts by the AccountManager)
Do you know of any library that takes care of all of this? It seems quite strange, isn't this a common issue?
Re: issue 1, as far as I can tell, an app is not "alive" until the user explicitly runs it for the first time. It will then still be "alive" until the end of the days, unless the user explicitly stops it using the Force stop button in Android's Applications management settings. He will then have to re-run manually the app for it to be able to receive broadcasts and stuff.