Bugsense on Android service

1.5k views Asked by At

Following Bugsense docs I've found that I need to add this code on Service onCreate:

BugSenseHandler.setup(context, "MY_API_KEY");

What it is that context variable? Do I need to pass main Activity reference to service constructor?

2

There are 2 answers

2
jelies On BEST ANSWER

context is your main activity or your service. Initialize BugSense calling BugSenseHandler.setup() with this.

public class MyService extends Service {
    ...
    @Override
    public void onCreate() {
        ...
        BugSenseHandler.setup(this, "MY_API_KEY");
        ...
    }
}
2
Codeversed On

The new API states this:

BugSenseHandler.initAndStartSession(MyService.this, "MY_API_KEY");

But pretty much the same as what jelies said in his answer

Don't forget about these as well:

Whenever you want to explicitly start the session, you can use the startSession method at the onStart method of your activity, as follows:

BugSenseHandler.startSession(MyService.this);

Whenever you want to close the session, you can use the closeSession method as follows:

BugSenseHandler.closeSession(MyService.this)

If you want to manually flush all the saved data, use the BugSenseHandler.flush(Context) method:

BugSenseHandler.flush(MyService.this);

Full doc for Bugsense is here: https://www.bugsense.com/docs/android You can apply this same logic to the service.