Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Enhance services

WonderCsabo edited this page Nov 7, 2014 · 19 revisions

Since AndroidAnnotations 2.4

You can enhance an Android Service with the @EService annotation:

@EService
public class MyService extends Service {

}

You can then start using most AA annotations, except the ones related to views and extras:

@EService
public class MyService extends IntentService {

  @SystemService
  NotificationManager notificationManager;

  @Bean
  MyEnhancedDatastore datastore;

  @RestService
  MyRestClient myRestClient;

  @OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
  UserDao userDao;

  public MyService() {
      super(MyService.class.getSimpleName());
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    // Do some stuff...

    showToast();
  }

  @UiThread
  void showToast() {
    Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
  }
}

You can start an enhances service via the inner Builder :

MyService_.intent(getApplication()).start();

Since AndroidAnnotations 3.0

The inner builder provide a stop method to stop this enhanced service:

MyService_.intent(getApplication()).stop();

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally