Detect Application Start - android

How can I detect the start of my application from a completely terminated stated (as if the user went to Manage Applications and forced stopped the app)? I don't mean like a first-time start up when the user first installed the app nor do I mean just in an Activity by using onStart() or onCreate().

I'm not sure exactly what you're asking, but Application.onCreate() will be called when the application is starting, before any other application objects have been created. So you could use that to detect when an app is started from a non-running state.

There's no way to detect when your app is "force stopped" from Android settings > apps page, or if the app is killed by another app. In both cases, the UNIX process is killed, and this severs the normal Android lifecycle.
Here's a post from dianne hackborn on the topic.

Related

How to close application after a while user does not interract with it in android?

I have android app using google maps API. When the app is running and user doesn't interract with it app still works and system doesn't go to sleep. However I need to close it if user doesn't interract with app some while. Is there any solutions?
There are a lot of posts regarding this topic already. A key point is that the OS handles this for you and it isn't necessary to force this behavior. If you really want to force it you could have a scheduler in your application triggered by the onStop() method. When the scheduled task executes you can then call one of the functions mentioned in the links provided below to kill the app.
How to quit android application programmatically
A timer that will kill android app after idle for certain time?

How to implement an app locker in android?

I am not satisfied with any of the app locker programs for Android that I have found and would like to create my own, but I am having trouble figuring out how to implement the actual lock. How does one go about implementing an app locker for Android?
There are two issues:
Detecting an Intent, usually from the launcher calling startActivity() or from an ad launching the market app (I'm not talking about broadcast intents -- I need to handle explicit Activity Intents).
The permissions for the locker apps I have seen all have "read system log files" and/or "retrieve running applications" which suggests they might use polling to look for app launches. I think I could do this, but I would like to find a better solution if possible.
Preventing the locked app from processing the Intent. I would prefer not to allow the locked app to run at all until the passcode is entered since it is impossible to tell what it might do when it starts up.
When running my current app locker, logcat shows "ActivityManager: Starting activity" first with the Intent for the locked app, then again with the Intent for the app locker, with nothing in between (separated by 10-20ms). If I enter the correct passcode then I see "ActivityManager: moveTaskToBack".
I have experimented with a test app which demonstrates that, using my current app locker, none of the Activity callbacks are invoked if the app is locked and I do not enter the correct passcode. So it appears to be preventing the app from starting (but I don't know how).
I could use ActivityManager.killBackgroundProcesses() to stop an app, but I have no guarantee that the app hasn't already started running by the time it gets "killed".
I might be able to use PackageManager.setApplicationEnabledSetting() to prevent an app from being instantiated, but I don't think that will take care of apps that are already running (which shouldn't be allowed to process new Intents or come to the foreground). I might be able to use killBackgroundProcesses() to stop all running locked processes so they would have to be re-instantiated. But I don't like this approach because other apps could mess with the Enabled settings and defeat the lock.
Optimally, the system would send every Intent to my app for inspection and allow me to stop or let it pass through, but I'm pretty sure that's not possible (at least, not for explicit intents to start activities from a launcher).
Does anyone know how app locker apps are implemented, or have any bright ideas on how it could be done?
I would look into the lifecycle. Once the app in question begins to load, some activity from that package will be added to the forefront activity.
A scan of the changes in forefront activities might do the trick.
http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

What happens when android app is "killed"

I have an app which has real time push notifications enabled. So my query is whenever any user tries to kill my app using "Advance Task Killer", my app goes into undefined state.
Undefined State: What i mean is my app doesn't gets completely terminated. The screen has data in a inconsistent state.
So is there way where i can take a user to login screen back whenever an app is killed. So that user wont see any undefined state.
Also want to know what happens to my app states after it gets killed so that i can fix the issue.
Help Appreciated.
It depends on the version of the platform. Prior to 2.2, third party applications like ATK could use an API that did the same thing as the "Force Stop" button in manage apps -- this kills all app processes, removes all tasks/activities, unregisters all alarms, removes all notifications, stops all services, etc. Basically make the application not running the same as if it was first installed, except its persistent data is still intact.
Needless to say, this tends to cause misbehavior of applications, so as of 2.2 other applications like ATK can no longer do this to your app. The API they were using is now only able to do the same thing that the out of memory killer does -- kill the application processes but only if they are in the background in a killable state. It can do no more than the normal out of memory killer, so as of 2.2 if your application is misbehaving due to an app like ATK being used on it then this is exposing an actual bug in the app that users will encounter through the normal use of their device.

Is it fine if I see my application from Settings App in the emulator has Force Close option enabled even tough I have come out of my application?

I have written a simple database program in android. It runs fine, there is no force close error. But I checked from my application from Settings App I see the Force Close option enabled, which implies that my application is still running in the background, even though I have completely came out from my application to the home screen by pressing back key. And moreover I am not using any services, alarm or broadcast things.
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Your application is alive until Android OS needs more memory and destroys it. What I have understood does Android start destroying activities before killing the whole application. This means that your application can be alive even if you have finished your activities.
Do not worry about this; Android OS is handling this extremely well.

How to exit an application completely in ANDROID

I have an application.In that if we press on home button app closes but when i launch the app it resumes where i stopped.I mean it do not closed completely.how to solve this problem.
When you press Home button, onDestroy method of the current activity is called. You can perform any shutdown operations there. Design of Android doesn't have a concept of explicit application shutdown, so that the user can continue on the same activity where he started.
You are trying to copy desktop application behavior (the application is shutdown explicitly) to Android with different usage patterns. While this is understandable, in most cases this will contradict to other applications behavior and will annoy users. So if you have anything to shutdown, do this in onDestroy method.
You can make your android app return to the root activity each time you open it by modifying your AndroidManifest.xml to include
android:clearTaskOnLaunch="true"
in the desired <activity> declaration.
Android does not allow you to terminate the application at any time. Application lifecycle is maintained by the Android OS itself. You are not supposed to meddle with it. Unlike desktop applications, Android application lifetime is determined by the OS itself. You can only end an activity.
For more Info refer to this bug http://code.google.com/p/android/issues/detail?id=1572
You need to call finish() in your onDestroy() method.

Categories

Resources