My application has functionality that send some content on server and (need) post it on facebook. It uses service for sending (for store data I'm use sqlite). Is it possible to handle facebook sessions e.t.c from service? I need it because time when content will be stored in DB and sending time can be different (may be connection will be lost on very long time and sending starts after connection will be available, but only sending service of my application will be active in this time). I can't find any solution for this case(and good solution will be great). Please help.
Users must explicitly grant permissions to the application, and Android services do not support Activity.startActivityForResult(), so it is not possible to open a Session or add new permissions via reauthorize from a service.
However, if you open/reauthorize a Session in one of the Activities of your app, the state of the Session is saved to SharedPreferences by default. If a Service creates a Session after this happens, it will end up in SessionState.CREATED_TOKEN_LOADED. If the Session.getState() is CREATED_TOKEN_LOADED, the Session is guaranteed to be able to openForRead() with no UI, and it is legal to pass a null Activity. The Session will have all the permissions granted that were saved by your Activity.
To simplify all this, your service can just call:
Session session = Session.openActiveSession(this);
This call returns null if no valid token was in SharedPreferences.
If your app UI has a settings area, an easy way to get the token stored in the first place is to include a LoginFragment as one of the settings panels. This will let your users log in/log out, and should save the Session state such that your service can pick it up if it is in the same app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With