Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Google Android

Integrating Facebook Into Android

Google Play Store, Amazon Kindle, Nook, and so many more

Integrating Facebook Into Android

Postby miyudreams » 28 Apr 2012, 08:39

Hi,

I am having some problems display the Facebook dialog box for posting to the user's Facebook feed. I have a HUD button that is pressed to post scores to Facebook. I wrote the callbacks to hook it up to Android's java code based on the wiki tutorial: http://www.stonetrip.com/developer/wiki/index.php?title=Hooks_and_Callbacks

In the static callback function I tried a lot of stuff.

I used Facebook's Android tutorial.https://developers.facebook.com/docs/mobile/android/build/
I tried Facebook's Hackbook code.https://developers.facebook.com/docs/guides/mobile/android_hackbook/
I tried extending AsyncTask. http://www.vogella.com/articles/AndroidPerformance/article.html
http://stackoverflow.com/questions/9425732/androidruntime-java-lang-runtimeexception-cant-create-handler-inside-thread-t

Facebook Authorization has no problems. But it will never display the dialog box.

I tried Facebook.request and Facebook.dialog.
I tried "stream.publish," "me/feed," and "feed."

Code: Select all
try
{
  Bundle parameters = new Bundle();
  parameters.putString("description", "Hello, testing");// the message to post to the wall
  //facebookClient.dialog(oThis, "stream.publish", parameters, this);
  //Utility.mFacebook.request("me/feed", parameters, "POST");
  Utility.mFacebook.dialog(oThis, "feed", parameters, new UpdateStatusListener());
  Log.d( "FacebookManager", "Message posted successfully.");
}
catch (Exception e)
{
  // TODO: handle exception
  Log.d( "FacebookManager", "Message post failed: " + e.getMessage());
}


Here is an AsyncTask java code:
Code: Select all
package com.companyname.gamename.Hackbook;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;


import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;
import com.companyname.gamename.Hackbook.SessionEvents.AuthListener;
import com.companyname.gamename.Hackbook.SessionEvents.LogoutListener;


public class FacebookManager extends AsyncTask<String, Void, Integer>
{
   private Facebook mFb;
    private String[] mPermissions;
    private Activity mActivity;
   
    public FacebookManager(final Activity activity, final Facebook fb, final String[] permissions)
    {
        mActivity = activity;
        mFb = fb;
        mPermissions = permissions;
   
    }
   
   @Override
    protected Integer doInBackground(String... sParams)
    {
        int count = sParams.length;
       
        /*if (!mFb.isSessionValid())
        {           
             mFb.authorize(mActivity, mPermissions, new LoginDialogListener());
            return 1;
        }*/
       
        return 0;
    }


   @Override
   protected void onPostExecute(Integer result)
   {
      fbPopupFeed();
   }
   
    private void fbPopupFeed()
    {       
       try
        {
            Bundle parameters = new Bundle();
            parameters.putString("description", "Hello, testing");
            //facebookClient.dialog(Jams.this, "stream.publish", parameters, this);
            //mFb.request("me/feed", parameters, "POST");
            //mFb.dialog(mActivity, "feed", parameters, new UpdateStatusListener());
            //mFb.dialog(mActivity, "me/feed", parameters, new UpdateStatusListener());
            mFb.dialog(mActivity, "stream.publish", parameters, new UpdateStatusListener());
            Log.d( "FacebookManager", "Message posted successfully.");
        }
        catch (Exception e)
        {
            // TODO: handle exception
           Log.d( "FacebookManager", "Message post failed: " + e.getMessage());
        }
    }
   
    /*
     * callback for the feed dialog which updates the profile status
     */
    public class UpdateStatusListener implements DialogListener 
    {
        @Override
        public void onComplete(Bundle values)
        {
            final String postId = values.getString("post_id");
            if (postId != null)
            {
               Log.d ( "FacebookManager", "Successfully posted." ) ;
            }
            else
            {
               Log.d ( "FacebookManager", "No Posts Made" ) ;
            }
        }

        @Override
        public void onCancel()
        {
           
        }
       
        @Override
        public void onFacebookError(FacebookError e) {
            e.printStackTrace();
        }

        @Override
        public void onError(DialogError e) {
            e.printStackTrace();
        }

    }
}


In the game file's .java file, I created an instance in "onCreate":

mFacebookManager = new FacebookManager(this, Utility.mFacebook, mPermissions);

In the callback function:
public static int fbShareScores( String sBody )
{
Log.d( Globals.sApplicationName, "fbShareScores: " + sBody);

oThis.mFacebookManager.execute(sBody);
return 0;
}

Please assist.
Thank you
miyudreams
Senior Boarder
Senior Boarder
 
Posts: 61

Re: Integrating Facebook Into Android

Postby error454 » 28 Apr 2012, 20:37

This is honestly a better question for stackoverflow.com.

The question is whether your onPostExecute is ever called. If it is then there's an issue with the facebook dialog, possibly to do with context I would guess. Try throwing up a toast in onPostExecute with the same context and see if it shows up.
error454
Platinum Boarder
Platinum Boarder
 
Posts: 653
Location: Portland, OR USA

Re: Integrating Facebook Into Android

Postby giggsy » 29 Apr 2012, 16:53

The answer probably is, because facebook is pure cra# :)
You download the official sdk, and then find out hours later that it's full of bugs or not yet released/live functions which lead to all kinds of errors.

We started playing around with it 2 weeks ago, and had to manually patch some facebook files in order to get them to work.
I'll try to get the changes and post em in the next week or so ...
User avatar
giggsy
Platinum Boarder
Platinum Boarder
 
Posts: 1093
Location: Austria

Re: Integrating Facebook Into Android

Postby miyudreams » 01 May 2012, 11:37

I'm glad to know someone else has tried it. onPostExecute does get called. I'll have to look into the Facebook SDK if you are suggesting that there's bugs in the SDK. Thanks for the tips! If you have a chance to post some code, that will be helpful.
miyudreams
Senior Boarder
Senior Boarder
 
Posts: 61

Re: Integrating Facebook Into Android

Postby miyudreams » 05 May 2012, 09:33

I managed to get Facebook up and running on my Android device and emulators. I noticed the game's .java file was using runOnUiThread, and this ended up working for me. No need to make any Facebook SDK changes. No need to extend AsyncTask.

See some sample code in this post: http://facebook.stackoverflow.com/questions/10427148/android-app-crashes-after-sharing-using-facebook-dialogs/10441776#10441776

Hope it saves other people from some headaches.
miyudreams
Senior Boarder
Senior Boarder
 
Posts: 61

Re: Integrating Facebook Into Android

Postby _geo_ » 07 May 2012, 09:58

Also good to know: if you get a blank (all white) permission screen (after authorization) you may have to patch the facebook sdk using this: https://github.com/ksperling/facebook-android-sdk/tree/ssl_errors - caused a lot of trouble for us.
fly yes, land no | Modern Alchemists | Blog | MASH | MATWEEN-AI
User avatar
_geo_
Gold Boarder
Gold Boarder
 
Posts: 193
Location: Austria


Return to Google Android