How to run activity in background in Android-ThrowExceptions

Exception or error:

In my application, I need to send the text message to the user after a certain interval. For that I need to run the message sending code in background. I am using the alarm manager to start activity at specific time.What to do? Is there another way to do this?

How to solve:

You could look into using Services which are basicly UI-less Activities.


For those who want to do it with an Activity, and not by using a service (there can be situations when this is necessary), I did it by starting the Activity the usual way, and then in the Activity’s onCreate I sent it to the background by calling moveTaskToBack(true) .

See Sending a running application to background programmatically

If you use this method, you should add an extra info to the intent by which you start the Activity, so that it knows in onCreate that it must send itself to the background. You do this by calling the Intent’s putExtra method. In the Activity’s onCreate, you get the calling Intent with getIntent, then retrieve the info you put there with getStringExtra, for instance (I used a string extra info). See the documentation for the Intent class.

Update august 2018: Note, however, that on some devices this can make your Activity blink (that is, briefly appear) on the screen before it’s sent to the background. It appears that the system treats the notification to send the task to the background way too late. If you call the method in onCreate, it should have plenty of time to know about it before it draws the Activity window. IMO this is a system flaw, it shouldn’t happen like this but it does.
So, anyway, if this is a concern and if you can, you’d probably better do it with a service.

### should be using Service to achieve this.They can run in background till explicitly closed.
IntentService is another alternative


This might help..
Add this to android manifest..:

uses-permission android:name=”android.permission.ACCESS_BACKGROUND_SERVICE”

Also use AsyncClass for your long/background processes..Like this.:


It is recommended to use Services to achieve that goal, but I’ve encountered similar situation when in need for something from another app – and the intent for the service wasn’t exported, so the only way to start the service is to start the activity – and to keep the user in my app I need to keep my activity in the foreground.
In that case it’s possible to start the other activity in an AsyncTask, and then in onPostExecute restart myself.

    (new AsyncTask<Void, Void, Void>() {
        boolean starting = false;
        protected Void doInBackground(Void... params) {
                Intent externalIntent = getPackageManager().getLaunchIntentForPackage("com.external...");
                if (externalIntent != null) {
                    starting = true;
            return null;

        protected void onPostExecute(Void aVoid) {
            if(starting) {
                Intent myApp = getPackageManager().getLaunchIntentForPackage("");
                if (myApp != null) {

Leave a Reply

Your email address will not be published. Required fields are marked *