3

I want to start my app at boot time, but want activity run in background at that time...

I have implemented BroadcastReceiver class for this, which is:

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

   }
}

but activity comes to front..

Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
  • 1
    "but want activity run in background at that time" -- since that is not strictly possible, perhaps you might consider explaining why you think that you need this. There may be a better solution for whatever problem you are trying to solve. – CommonsWare Jan 29 '14 at 17:13
  • what you want exactly when you fire a intent activity will always comes to front.... start a service add your code in service it will work in background – rajahsekar Jan 29 '14 at 17:14
  • I call a static method (in BroadcastReceiver) which is in MainActivity Class whenever SMS is Received, but when device is rebooted all activities and methods are destroyed, therefore I want to startActivities at boot time as those metods can be called... – Naveed Ali Jan 29 '14 at 17:19
  • If your static method has nothing to do with activity it's better to move it in another class. The main issue in your case is having the method in a wrong place. – azertiti Jan 29 '14 at 17:31
  • method is related with the Views of activity... – Naveed Ali Jan 29 '14 at 17:35
  • 1
    "but when device is rebooted all activities and methods are destroyed" -- your process can go away at any time, for any reason, based on user action or OS decision. "therefore I want to startActivities at boot time as those metods can be called" -- and your process will be terminated a few seconds afterwards, taking you back to your original state. "method is related with the Views of activity" -- a static method should not be "related with the Views of activity", as that will typically represent a memory leak. – CommonsWare Jan 29 '14 at 17:55
  • Thanks to All, I got solution to implement my desired action, no need to run activities in background... – Naveed Ali Jan 31 '14 at 06:32

3 Answers3

2

For what you want the achieve... Activity can't be run in background, as it will start and will be visible to the user.

What you may do is... Start a service at boot time which after few seconds starts your activity or whenever you want.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
1

For background tasks we use services on android.This way you can have your app performing your operations in the background with out bringing the app to foreground.

Madala
  • 291
  • 3
  • 8
0

If you want something to run in the background you'll have to create a service, from which you can then start your main acrivity at a certain time

japk
  • 619
  • 6
  • 10