Advertisement
kusha45

facebook

May 17th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package com.pedometer;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.content.SharedPreferences.Editor;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.widget.Toast;
  12.  
  13. import com.facebook.android.DialogError;
  14. import com.facebook.android.Facebook;
  15. import com.facebook.android.Facebook.DialogListener;
  16. import com.facebook.android.FacebookError;
  17.  
  18. public class ShareOnFacebook extends Activity{
  19.  
  20. private static final String APP_ID = "106320872838957"; //322797887779966
  21. private static final String[] PERMISSIONS = new String[] {"publish_stream"};
  22.  
  23. private static final String TOKEN = "access_token";
  24. private static final String EXPIRES = "expires_in";
  25. private static final String KEY = "facebook-credentials";
  26.  
  27. private Facebook facebook;
  28. private String messageToPost;
  29.  
  30. public boolean saveCredentials(Facebook facebook) {
  31. Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
  32. editor.putString(TOKEN, facebook.getAccessToken());
  33. editor.putLong(EXPIRES, facebook.getAccessExpires());
  34. return editor.commit();
  35. }
  36.  
  37. public boolean restoreCredentials(Facebook facebook) {
  38. SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
  39. facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
  40. facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
  41. return facebook.isSessionValid();
  42. }
  43.  
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47.  
  48. facebook = new Facebook(APP_ID);
  49. restoreCredentials(facebook);
  50.  
  51. requestWindowFeature(Window.FEATURE_NO_TITLE);
  52.  
  53. setContentView(R.layout.facebook_dialog);
  54.  
  55. String facebookMessage = getIntent().getStringExtra("facebookMessage");
  56. if (facebookMessage == null){
  57. facebookMessage = Pedometer.facebookText;
  58. }
  59. messageToPost = facebookMessage;
  60. }
  61.  
  62. public void doNotShare(View button){
  63. finish();
  64. }
  65.  
  66. public void share(View button){
  67. if (! facebook.isSessionValid()) {
  68. loginAndPostToWall();
  69. }
  70. else {
  71. postToWall(messageToPost);
  72. }
  73. }
  74.  
  75. public void loginAndPostToWall(){
  76. facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
  77. }
  78.  
  79. public void postToWall(String message){
  80. Bundle parameters = new Bundle();
  81. parameters.putString("message", message);
  82. parameters.putString("description", "topic share");
  83. try {
  84. facebook.request("me");
  85. String response = facebook.request("me/feed", parameters, "POST");
  86. Log.d("Tests", "got response: " + response);
  87. if (response == null || response.equals("") ||
  88. response.equals("false")) {
  89. showToast("Blank response.");
  90. }
  91. else {
  92. showToast("Message posted to your facebook wall!");
  93. }
  94. finish();
  95. }
  96. catch (Exception e) {
  97. showToast("Failed to post to wall!");
  98. e.printStackTrace();
  99. finish();
  100. }
  101. }
  102.  
  103. class LoginDialogListener implements DialogListener {
  104. public void onComplete(Bundle values) {
  105. saveCredentials(facebook);
  106. if (messageToPost != null){
  107. postToWall(messageToPost);
  108. }
  109. }
  110. public void onFacebookError(FacebookError error) {
  111. showToast("Authentication with Facebook failed!");
  112. finish();
  113. }
  114. public void onError(DialogError error) {
  115. showToast("Authentication with Facebook failed!");
  116. finish();
  117. }
  118. public void onCancel() {
  119. showToast("Authentication with Facebook cancelled!");
  120. finish();
  121. }
  122. }
  123.  
  124. private void showToast(String message){
  125. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement