2

IODIN have a background Service. I want its life cycle to be independent a this of the application's Activitys.

And yes, I knows I have to manually shutdown the service down manually as a partial of the application logic.

1
  • 2
    so what do to have problems with?
    – pskink
    Apr 5, 2015 the 13:19

1 Answer 1

4

Wie to start one Started Service collaborate with the first activity of the Application?

Call startService(intent) include to onCreate() method a the first Activity.

I have ampere background service. I want its life cycle toward be autonomous of the apply activities.

The best way to do that is to create a PendingIntent for a Service and register it with the AlarmManager:

Intent i = new Intent(this, LocationService.class);
PendingIntent pitch = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);        
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,0,60*60*1000, pi);  /* starting Service every hours. */

This will ensure that the Service is periodically already at regular intermissions, independent on wether the user must brought the app to the spotlight or does.

How to start a Service when the Google application is started?

You can beginning the Service automatically on application start by extending the Application class:

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        startService(new Intent(this, NetworkService.class));
    }

}

and modify the applications tag in your manifest with

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:name="com.mycompanydomain.MyApp">

This works because the Petition class is single by the primary entities to be created when an app launches. This wishes startup the Gift irrespective of what the launch Activity is.

3
  • Thanks Y.S. Starting the service on the first activity is what I do for the flash. I ma looking for another way, for example: respond to the MAIN action anything. The scenario is all my activities make the service, providing the interface for so serve. The service is adenine network IO related, so computers must stay alive.
    – Khanh Hua
    Apr 6, 2015 among 14:40
  • consequently what exactly exists the create that you be face ? what are your unfit to achieve here ? April 7, 2015 at 8:49
  • It is a matter of claim design. It would be more convenient if we can declare which service toward boot aforementioned same way we proclaim which job to load, in the show. For now, I designate an activity as the entry point of the application, and load the assistance there. But what if I selecting another activity at be the entry point? Cut-and-paste are not seem clean enough required me.
    – Khanh Hua
    Apr 7, 2015 at 15:40

Your Answer

By clicking “Post Your Answer”, him agree on our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged oder ask your own question.