Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:22:18+00:00 2026-06-03T02:22:18+00:00

In the Facebook android sdk docs , the example presented uses Activity to authenticate

  • 0

In the Facebook android sdk docs, the example presented uses Activity to authenticate the user. Is it possible to do the same from a Service? An example code will be quite handy.
thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:22:20+00:00Added an answer on June 3, 2026 at 2:22 am

    It’s impossible to authorize within Facebook object anywhere else except Activity.

    You can try to obtain access token via some web-service and initialize your

    Facebook facebook
    

    with that. Still, you can authorize your session within activity and pass it through binder.

    Here is my example.

    This is a service

    public class ClientService extends Service {
    
        //Facebook object to operate from service
        private Facebook facebook;
    
    ...
    
        private final ClientBinder clientBinter = new ClientBinder();
    
        /** Binder to service */
        public class ClientBinder extends Binder {
    
            public ClientService getService() {
                return ClientService.this;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return clientBinter;
        }
    
        public void setFacebook(Facebook f) {
            ClientService.this.facebook = f;
        }
    
        public boolean loggedFacebook() {
            if (facebook == null)
                return false;
            return facebook.isSessionValid();
        }
        ...
    
    }
    

    And this is usage from activity

        public class SomeActivity extends Activity{
    
            private ClientService clientService;
            private ServiceConnection clientConnection = new ServiceConnection() {
    
                public void onServiceDisconnected(ComponentName name) {
                    mBound = false;
                }
    
                public void onServiceConnected(ComponentName name, IBinder service) {
                    ClientService.ClientBinder binder = (ClientService.ClientBinder) service;
                    clientService = binder.getService();
                    mBound = true;
                    if (facebook != null)
                        if (facebook.isSessionValid())
                            clientService.setFacebook(facebook);
                }
            };
    
        @Override
        protected void onStart() {
            super.onStart();
            Intent intent = new Intent(this, ClientService.class);
            bindService(intent, clientConnection, Context.BIND_AUTO_CREATE);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            if (mBound) {
                unbindService(clientConnection);
                mBound = false;
            }
    
        }
    
    
    .................
    
        //And HERE IS MAGIC
        .....
        private void logINFacebook() {
                if (facebook.isSessionValid())
                    return;
    
                facebook.authorize(this,
                        getResources().getStringArray(R.array.facebookPermissions),
                        new DialogListener() {
                            public void onFacebookError(FacebookError e) {...}
    
                            public void onError(DialogError e) {...}
    
                            public void onComplete(Bundle values) {
                                if (mBound) {
                                    clientService.setFacebook(facebook);
                                }
    
                                //Save to shared preferences(optional)
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putString(FACEBOOK_TOKEN_PREFNAME,
                                        facebook.getAccessToken());
                                editor.putLong(FACEBOOK_EXPIRES_PREFNAME,
                                        facebook.getAccessExpires());
                                editor.commit();
                            }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            facebook.authorizeCallback(requestCode, resultCode, data);
            super.onActivityResult(requestCode, resultCode, data);
        }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is from the Facebook Android SDK, and is used for all
I try to connect to Facebook throught Facebook API, I follow this example: https://github.com/facebook/facebook-android-sdk/tree/master/examples/simple
I have an application that uses the Facebook SDK for Android to login into
Good evening. I'm developing an android application that uses Facebook SDK. I've no problem
I try to follow the facebook-android tutorial from here: https://developers.facebook.com/docs/mobile/android/build/#sample I am working in
i am using the facebook android sdk, which i just downloaded from github. i
Facebook Android SDK when running example apps gives 11-14 23:17:58.484: ERROR/HttpOperation.run(1139): class java.net.UnknownHostException api.facebook.com
I'm working on an Android app, specifically one that uses the Facebook Android SDK.
i downloaded the facebook android application example from github and imported it using the
I've been working with the facebook-android-sdk and integrated posting a message to the user's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.