I have an android app that needs to run an async task every time it starts up... Not every time I open an activity but every time the app starts up. I don't think the activity life-cycle will help me here so I'm looking for suggestions.
-
possible duplicate of [How can one detect an Android application launching?](http://stackoverflow.com/questions/6182046/how-can-one-detect-an-android-application-launching) – d.moncada Apr 25 '14 at 17:00
-
Not sure how to implement this, you may need to call on a utility-like class in all of your necessary onCreate/onResumes that holds your AsyncTask and some return method. – zgc7009 Apr 25 '14 at 17:02
-
@d.moncada It is, but that was also asked over 3 years ago and android is ever evolving. So i thought by now there maybe a solution – jcaruso Apr 25 '14 at 18:33
-
@d.moncada that person wants to know when other persons apps launch, i want to know when my own launches. – jcaruso Apr 25 '14 at 19:51
-
@jcaruso Ahh okay, looks like I overlooked your question. Sorry about that! Glad you found a solution though. – d.moncada Apr 25 '14 at 20:00
2 Answers
Make a subclass of Application, override its onCreate()
and then tell Android to use your subclass by setting its full classname as the value of 'android:name' in <application />
in your Android manifest.
<application
android:name="com.yourpackage.YourApplicationSubclass"
....
/>
Now with more detail:
android.app.Application
is the base class that represents your application. While you generally don't need to subclass Application, doing so gives you the opportunity to get your hooks into your application's lifecycle, rather than just your Activities', Intents', and Services'.
All you need to do is create a new subclass of Application:
import android.app.Application;
public class MyApplication extends Application
{
@Override
public void onCreate() {
// Do stuff when the application starts
}
}
And then updated your AndroidManifest.xml as I described above.

- 3,031
- 12
- 12
-
-
1
-
thanks, Last question. In my AsyncTask theres a `a.getContentResolver().insert(StatusProvider.CONTENT_URI_SCHEDULE, values);` where `a` is the activity. How can i define an activity there? – jcaruso Apr 25 '14 at 19:00
-
I think i'll do as @Hanzo says and create a Boolean value there and kick these off from the actives. – jcaruso Apr 25 '14 at 19:05
-
You can get your content resolver from any `Context` (an Activity is a Context), and -- lucky for you -- `Application` is also a Context. :) So just give your AsyncTask the Application object instead of the Activity and you should be good. You may need to revise the AsyncTask so it takes a `Context` instead of an `Activity`, but that shouldn't have any consequences. – adpalumbo Apr 25 '14 at 19:38
-
This is so helpful and to think they want to mark it as a duplicate! This is a real answer rather than the one they are saying its a duplicate of. – jcaruso Apr 25 '14 at 19:48
Mark your start activity as main in your Manifest like this
<activity
android:name="com.your.package.name.activityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Add a boolean extra to every intent that his target be the main start activity. When the main activity starts if the intent not has the boolen extra then create the async task. If exist the boolean extra means that have parent and not is a start of app.
Here have more info about Async Task
http://developer.android.com/reference/android/os/AsyncTask.html

- 1,839
- 4
- 30
- 51