Download: http://www.filefactory.com/file/cbd661e/n/Facebook-Example.zip
Steps involved:
1. Apply for a Facebook Application ID (APP_ID)
2. Include the Facebook APP_ID in your Android Application
Facebook mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
3. Make a call to Facebook for single-sign on authorization
4. In your onActivityResult() function
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
File: facebook_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:gravity="center_horizontal"> <com.facebook.android.LoginButton android:id="@+id/login" android:src="@drawable/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="30dp" /> <TextView android:id="@+id/txt" android:text="@string/hello" android:textColor="@color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/uploadButton" android:text="@string/FACEBOOK_UPLOAD" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="20dp" android:paddingLeft="20dp" android:layout_margin="20dp" /> <Button android:id="@+id/requestButton" android:text="@string/REQUEST" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="20dp" android:paddingLeft="20dp" android:layout_margin="20dp" /> <Button android:id="@+id/postButton" android:text="@string/FACEBOOK_WALL_POST" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="20dp" android:paddingLeft="20dp" android:layout_margin="20dp" /> <Button android:id="@+id/deletePostButton" android:text="@string/FACEBOOK_DELETE_POST" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="20dp" android:paddingLeft="20dp" android:layout_margin="20dp" /> </LinearLayout>
File: Main.java
package com.facebook.android;
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
import org.json.JSONException; import org.json.JSONObject;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;
import com.facebook.android.Facebook.DialogListener; import com.facebook.android.SessionEvents.AuthListener; import com.facebook.android.SessionEvents.LogoutListener;
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private LoginButton mLoginButton;
private TextView mText;
private Button mRequestButton;
private Button mPostButton;
private Button mDeleteButton;
private Button mUploadButton;
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(tag, getResources().getString(R.string.CREATING_VIEW));
mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
setContentView(R.layout.facebook_login_view);
mLoginButton = (LoginButton) this.findViewById(R.id.login);
mText = (TextView) Main.this.findViewById(R.id.txt);
mRequestButton = (Button) findViewById(R.id.requestButton);
mPostButton = (Button) findViewById(R.id.postButton);
mDeleteButton = (Button) findViewById(R.id.deletePostButton);
mUploadButton = (Button) findViewById(R.id.uploadButton);
mFacebook.authorize(this, new DialogListener()
{
@Override
public void onComplete(Bundle values)
{
}
@Override
public void onFacebookError(FacebookError error)
{
}
@Override
public void onError(DialogError e)
{
}
@Override
public void onCancel()
{
}
});
//
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
mLoginButton.init(this, mFacebook);
mRequestButton.setOnClickListener(this);
mRequestButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);
mUploadButton.setOnClickListener(this);
mUploadButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);
mPostButton.setOnClickListener(this);
mPostButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
//
public class SampleAuthListener implements AuthListener
{
@Override
public void onAuthSucceed()
{
mText.setText("You have logged in! ");
mRequestButton.setVisibility(View.VISIBLE);
mUploadButton.setVisibility(View.VISIBLE);
mPostButton.setVisibility(View.VISIBLE);
}
@Override
public void onAuthFail(String error)
{
mText.setText("Login Failed: " + error);
}
}
public class SampleLogoutListener implements LogoutListener
{
@Override
public void onLogoutBegin()
{
mText.setText("Logging out...");
}
@Override
public void onLogoutFinish()
{
mText.setText("You have logged out! ");
mRequestButton.setVisibility(View.INVISIBLE);
mUploadButton.setVisibility(View.INVISIBLE);
mPostButton.setVisibility(View.INVISIBLE);
}
}
public class SampleRequestListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
try
{
// process the response here: executed in background thread
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String name = json.getString("name");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
Main.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mText.setText("Hello there, " + name + "!");
}
});
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public class SampleUploadListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
try
{
// process the response here: (executed in background thread)
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String src = json.getString("src");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
Main.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mText.setText("Hello there, photo has been uploaded at \n" + src);
}
});
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public class WallPostRequestListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
Log.d("Facebook-Example", "Got response: " + response);
String message = "<empty>";
try
{
JSONObject json = Util.parseJson(response);
message = json.getString("message");
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
final String text = "Your Wall Post: " + message;
Main.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mText.setText(text);
}
});
}
}
public class WallPostDeleteListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
if (response.equals("true"))
{
Log.d("Facebook-Example", "Successfully deleted wall post");
Main.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mDeleteButton.setVisibility(View.INVISIBLE);
mText.setText("Deleted Wall Post");
}
});
}
else
{
Log.d("Facebook-Example", "Could not delete wall post");
}
}
}
public class SampleDialogListener extends BaseDialogListener
{
@Override
public void onComplete(Bundle values)
{
final String postId = values.getString("post_id");
if (postId != null)
{
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
mDeleteButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mAsyncRunner.request(postId, new Bundle(), "DELETE", new WallPostDeleteListener(), null);
}
});
mDeleteButton.setVisibility(View.VISIBLE);
}
else
{
Log.d("Facebook-Example", "No wall post made");
}
}
}
/*
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v)
{
if (v == mLoginButton)
{
}
if (v == mRequestButton)
{
mAsyncRunner.request("me", new SampleRequestListener());
}
if (v == mUploadButton)
{
Bundle params = new Bundle();
params.putString("method", "photos.upload");
URL uploadFileUrl = null;
try
{
uploadFileUrl = new URL("http://www.facebook.com/images/devsite/iphone_connect_btn.jpg");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn = (HttpURLConnection) uploadFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
byte[] imgData = new byte[length];
InputStream is = conn.getInputStream();
is.read(imgData);
params.putByteArray("picture", imgData);
}
catch (IOException e)
{
e.printStackTrace();
}
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
if (v == mPostButton)
{
mFacebook.dialog(Main.this, "feed", new SampleDialogListener());
}
if (v == mDeleteButton)
{
}
}



Supper Very Very Helpfull For Me
Thankssssssss YAr
Plz Send Me This Project Sourch Code
If Possible
Thanks Again
Hi,
Could you just provide the complete source code, i am unable to download that .zip file
Thanks
plz send me complete file
it’s not working… I’m new to Android… I copy and paste all the codes. But it’s not working..
send ur mail id ,i will send the files
Hi shahzad..
can u send me that complete code t my mail..
my mail id is : mvmounika305@gmail.com
Regards,
Mounika
please send complete code,
hi..shahzad can u send me the all files ..its really helpful for me..plz..
mail is.. haria.airah@gmail.com
Regards,
Hari
hi please send me the complete code for me
my mail id : kaniramasamy@yahoo.com
hi can u send me that complete code to my mail
mail id:kanicse42@gmail.com
Regards,
kanimozhi
Hi shahzad..
can u send me that complete code my mail..
pprashant1227@gmail.com
satishmails87@gmail.com … please send me the code….
hi Shahzad, can send me the code to my mail plzz mail id: psowmya88@yahoo.com
hi……shahzad send me complete code for my mail…. help me
I need complete code can you just forward me to the below id
shivaprakashpatil41@gmail.com
Hi shahzad..
can u send me that complete code t my mail..
mailid:chindamparamesh@gmail.com
hi this is britto Shahzad.. Pls send ur code to my mail id.. brittsdebugger@gmail.com
gud
Hi
Thanks a lot for sharing this knowledgeable piece of code….great work.
Thanks and regards,
Sumith.M.P
It is working………………..
it is really helpful in making application to me…….but how will i pick cantact and phone number from facebook contacts
super its working fine………
can u send that source code to my mail id navulurideepak@gmail.com please
it’s good
plz send me the source code
my email is bpsingh216@gmail.com
i copied the code but it doesnt work. plz email me the complete code
Kindly email that source code, zip file can not b downloaded
please sen me the source code iam unable to download it my id is tahir90.webdeveloper@gmail.com
Hi,
I am unable to download will you please help me how to download that zip plese
Hi,
I am unable to download will you please help me how to download that zip plese on my emailId
mvishnoi35@gmail.com
Link is outdated since they closed down mediafire etc.
github: https://github.com/w2davids/Ghostdrive
click on zip
May I have the complete code by mail please
my mail is : b.dvloper@gmail.com
Regards
Please send me demo on geniousnirav@gmail.com
Hi !
Pl. sent me the proj source code to velansms@gmail.com
Thanks indeed
share with me…..my mail id palepurajesh@gmail.com
Hi,
I am unable to download will you please help me how to download that zip plese
Reply //////Rajesh
HI,
Can u please send the source code to my mail id navulurideepak@gmail.com
Hi shahzad..
can u send me that complete code t my mail..
my mail id is :adam@osvin.com
Regards,
Adam
Hi sir , I am srinivas (Android App Developer). I gone through ur tutorials. it helping me a lot. I am facing problem with EMail integration with android app.
Regards,
Srinivas Nidadavolu
nsc462@gmail.com
please send me the source code…unable to download..
suzzane0512@gmail.com
please send me the source code my mail id is tiwarialok786@gmail.com
please send me the source code iam unable to download it my id is
vignesh89cse@gmail.com
hi very nice coding please send the source code my mail id m.dhamodhar@gmail.com
link for downloading code is not working.please send the code on
prashantwankhede89@gmail.com
I am getting error with init() method…. Kindly send me complete project….
Plz send me the source code
@ sunil.gupta676@gmail.com
Pingback: Android Facebook SDK : post message without showing facebook page | PHP Developer Resource
can u plz tell me where i can get the sample code
please send me the source code…unable to download..
pprashant1227@gmail.com
Please forward me the source at vikashkumar801@gmail.com , vjalans@yahoo.co.in
hello nice coding please send the source code my mail id
sweaty_321@yahoo.com
thankyou so much
It’s really good sample project..its working too good…thanks lot
please any send me the source code to joswaprabhu@gmail.com
Any one send me code custom control with Facebook how do design in mono for android framework
please any one send the source code to chandruu74@yahoo.co.in
Hi ….
i also cant get zip file will u plz help me ……
if possible plz mail me i need ur help
Riddhi
my mail isd is fly2riddhitrivedi@gmail.com
might you please send me the source code on my email id anilchahal1234@gmail.com
Hi shahzad..
can u send me that complete code t my mail..
my mail id is:benild.r@gmail.com
complete code plz
Error: MyApp in misconfigured for Facebook login. Prees Okay to go back to the application without connecting it to Facebook.
Hi shahzad..
can u send me that complete code to my mail..
my mail id is:srisailamnimmala@gmail.com
pls send me full codes to vasanthgopal89@gmail.com
pl send me also the source code if it works with uploading image. I tried to work out with your example but it doesn’t work with mine. So please send me the code at wellesly@azinova.info
Can u plz send me the source code in my mail id faruk.it.09@gmail.com
Please send me too lagrawal.nri@gmail.com
please send me too helina@rathnasoft.com
hi,
can u help me,
i want to post photo with tagging my friends along with msg using fb sdk from android . how to do that. please help me its urgent.
Thanks in advance,plz send this complete code in my email id irfan.143rkl@gmail.com
Pingback: How can I go to new screen(Intent) after logging successfully in Facebook android video
Pingback: How can I go to new screen(Intent) after logging successfully in Facebook android : Android Community - For Application Development
Hi,can you please send complete code in my mail is jwala_ch@yahoo.com…i am trying to execute code but i am unlucky….Thanks..
hi nice tutorial, can you send me the complete code to devandroid@gmail.com please…
plz send me the code at harisahmed6182@gmail.com thanku
plz send me the complete code at catch22535@gmail.com
Thnx
please send me the source code…unable to download..
My mail id mnknagaganesh@gmail.com
please send me the source code…unable to download..
My mail id geomobapk@gmail.com
please send me nain_cse@yahoo.com
hey can i incorporate your code into my project
if yes then what changes would i have to make
i have finished all the prerequisites.
please reply asap
if it is work properly the please send me complete code..teraiyamayurcontact@yahoo.com
Hi,
Can you send the complete code to me asap.My Mail id is:
srilakshmipopuri@yahoo.co.in
Hi, its not downloable, can u pls send the full source code at sachin.j2217@gmail.com
hey i incorporated your code into my small project. When i used initially it was displaying the post request upload buttons but now when i use it on the emulator it doesn’t show the buttons
please advise me what is the prob
i really need it to work
please send to me full source code…rickyrobiansyah@gmail.com
Can u send me your full source code? I would like to go through all the tutorial.
hi i am new to android and i dont know how to implement this in my application, so can u send me full source code?
my mail id is
john25586@gmail.com
thanx in adv.