How we can use Service class?
RupeshVM opened this issue · 6 comments
RupeshVM commented
I want to insert data inside local db from service class.
amitshekhariitbhu commented
I will add the example soon.
RupeshVM commented
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 ***@***.***> 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
<#17 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/APwEXpkLKB1uYDmBsXhAbr7q18YZdaiMks5tSZA-gaJpZM4R8atR>
.
159159951 commented
Im facing with your issues.
Have any body can solve this problem?
amitshekhariitbhu commented
We have the service class example in this project.
dseerapu commented
Follow below steps to inject Data Manager instance in service class
- 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();
}
- 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();
}
- Finally Inject
DataManager instance
inIdentifyAppClearedFromRecentsService
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.
dpnkrg commented
db insert or send data from db to server example?