android-mvvm-architecture icon indicating copy to clipboard operation
android-mvvm-architecture copied to clipboard

How we can use Service class?

Open RupeshVM opened this issue 7 years ago • 6 comments

I want to insert data inside local db from service class.

RupeshVM avatar Feb 07 '18 09:02 RupeshVM

I will add the example soon.

amitshekhariitbhu avatar Feb 07 '18 11:02 amitshekhariitbhu

Thank you sir,

Now, any suggestions for how can i access Data Manager instance in service class.

On Wed, Feb 7, 2018 at 5:29 PM, AMIT SHEKHAR [email protected] wrote:

I will add the example soon.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MindorksOpenSource/android-mvvm-architecture/issues/17#issuecomment-363747845, or mute the thread https://github.com/notifications/unsubscribe-auth/APwEXpkLKB1uYDmBsXhAbr7q18YZdaiMks5tSZA-gaJpZM4R8atR .

RupeshVM avatar Feb 07 '18 12:02 RupeshVM

Im facing with your issues. Have any body can solve this problem?

159159951 avatar Apr 17 '18 04:04 159159951

We have the service class example in this project.

amitshekhariitbhu avatar Apr 17 '18 06:04 amitshekhariitbhu

Follow below steps to inject Data Manager instance in service class

  1. Create ServiceBuilderModule class
@Module
public abstract class ServiceBuilderModule {

   // Note: Add all your application service classes here
   // As an example I am adding IdentifyAppClearedFromRecentService
    @ContributesAndroidInjector
    abstract IdentifyAppClearedFromRecentsService bindNetworkJob();
}
  1. Add ServiceBuilderModule class in your application component. In my application: I haveAppComponent class as below
@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        ActivityBuilderModule.class,
        ServiceBuilderModule.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(SampleApp sampleApp);

    DataManager getDataManager();
}
  1. Finally Inject DataManager instance in IdentifyAppClearedFromRecentsService class as below
public class IdentifyAppClearedFromRecentService extends Service {

    @Inject
    NLearnDataManager dataManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        //Code here
        dataManager.stopSync();
        stopSelf();
    }
}

Note: Don't forget to add SampleApp class implement HasServiceInjector as below

public class SampleApp extends Application implements HasActivityInjector,
        HasServiceInjector{

    @Inject
    DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;

    @Inject
    DispatchingAndroidInjector<Service> serviceDispatchingAndroidInjector;

    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        initializeDaggerComponent();
    }

    private void initializeDaggerComponent() {

        appComponent = DaggerAppComponent.builder().application(this).build();
        appComponent.inject(this);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return activityDispatchingAndroidInjector;
    }

    @Override
    public AndroidInjector<Service> serviceInjector() {
        return serviceDispatchingAndroidInjector;
    }
}

@rupvm might this solves your problem.

dseerapu avatar Apr 09 '19 11:04 dseerapu

db insert or send data from db to server example?

dpnkrg avatar Feb 10 '20 09:02 dpnkrg