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 8643097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:56:52+00:00 2026-06-12T11:56:52+00:00

I am trying to implement facebook login and wall post in Android, so I

  • 0

I am trying to implement facebook login and wall post in Android, so I created a non-activity class that handles everything. I saw all these examples where they use this method – onActivityResult but I don’t know if I have to use it or why it’s so important. Code works without it as long as I don’t have the facebook app installed on the phone and I wonder if onActivityResult have anything to do with it. PS: I’m pretty sure I generated the hash key corectly. Thank you. 🙂

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.infobest.praiser.R;
import com.infobest.praiser.utils.Constants;

/**
 * Functionality for facebook sharing
 * 
 * @author oana_balaceanu
 * 
 */
public class ShareFacebook
{
    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private Facebook facebook;
    private String messageToPost;
    private Context ctx;

    public ShareFacebook(String messageToPost, Context ctx)
    {
        this.messageToPost = messageToPost;
        this.ctx = ctx;

    }

    public boolean saveCredentials(Facebook facebook)
    {
        Editor editor = ctx.getSharedPreferences(Constants.KEY, Context.MODE_PRIVATE).edit();
        editor.putString(Constants.TOKEN, facebook.getAccessToken());
        editor.putLong(Constants.EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook)
    {
        SharedPreferences sharedPreferences = ctx.getSharedPreferences(Constants.KEY,
            Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(Constants.TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(Constants.EXPIRES, 0));
        return facebook.isSessionValid();
    }

    public void share()
    {
        facebook = new Facebook(Constants.APP_ID);
        restoreCredentials(facebook);

        if (!facebook.isSessionValid())
        {
            loginAndPostToWall();
        }
        else
        {
            postToWall(messageToPost);
        }
    }


    public void loginAndPostToWall()
    {
        facebook.authorize((Activity) ctx, PERMISSIONS,
            (DialogListener) new LoginDialogListener());
    }

    public void postToWall(String message)
    {
        FacebookPoster fp = new FacebookPoster();
        fp.execute(message, null, null);
    }

    private class FacebookPoster extends AsyncTask<String, Object, Object>
    {

        @Override
        protected Object doInBackground(String... message)
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", message[0]);
            parameters.putString("link", ctx.getResources().getString(R.string.rateLink));
            parameters.putString("picture", ctx.getResources().getString(R.string.linkIconPicture));
            try
            {
                facebook.request("me");
                String response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || response.equals("false"))
                {
                    return ctx.getResources().getString(R.string.facebookError);
                }
                else
                {
                    return ctx.getResources().getString(R.string.facebookSuccess);

                }
            }
            catch (Exception e)
            {
                Log.d("ShareOnFacebook", e.toString());
                return ctx.getResources().getString(R.string.facebookError);
            }
        }

        @Override
        protected void onPostExecute(Object result)
        {

            super.onPostExecute(result);
            showToast(result);
        }


    }


    class LoginDialogListener implements DialogListener
    {
        public void onComplete(Bundle values)
        {
            saveCredentials(facebook);
            if (messageToPost != null)
            {
                postToWall(messageToPost);
            }

        }

        public void onFacebookError(FacebookError error)
        {
            showToast(ctx.getResources().getString(R.string.facebookError));

        }

        public void onError(DialogError error)
        {
            showToast(ctx.getResources().getString(R.string.facebookError));

        }

        public void onCancel()
        {
            showToast(ctx.getResources().getString(R.string.facebookCancel));

        }
    }

    private void showToast(Object message)
    {
        Toast.makeText(ctx, message.toString(), Toast.LENGTH_SHORT).show();
    }


}
  • 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-12T11:56:54+00:00Added an answer on June 12, 2026 at 11:56 am

    If you test your app on emulator or smartphone with installed Facebook app, login will fail because of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to implement a login with facebook/twitter functionality in my app, i read
I am trying implement a photo gallery similar to facebook in Android. I was
I am trying to implement Facebook Login functionality to my website. I followed the
I am trying to implement the Facebook Login and Logout in my website. I
I am trying to implement facebook like functionality using android webview. It is working
I'm trying to implement a Login with Facebook using the Open Graph API. So
I am trying to implement Facebook Connect functionality into a website that I am
I am trying to implement Facebook into my Android app. Everytime though, the facebook
Hi i was trying to implement facebook login for my django app using django-facebook
I'm trying to implement the Facebook login for one of my applications and the

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.