This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 24th, 2012  |  syntax: None  |  size: 4.57 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.    public void onCreate(Bundle savedInstanceState) {
  2.        
  3.         super.onCreate(savedInstanceState);
  4.         setContentView(R.layout.activity_login);
  5.         facebookLogin();
  6.     }
  7. public void facebookLogin()
  8.     {
  9.         shareAppPreferences = new AppPreferences(getApplicationContext());
  10.          facebook =  new Facebook("APP KEY");//((GlobalVars)getApplicationContext()).facebook;
  11.          Log.d("MyTag","access key: "+ shareAppPreferences.getAccessKey());
  12.          String access_token = shareAppPreferences.getAccessToken();
  13.          Long expires = shareAppPreferences.getAccessExpires();
  14.          Log.d("MyTag","token:" +access_token);
  15.          if(access_token != null) {
  16.              facebook.setAccessToken(access_token);
  17.          }
  18.          if(expires != 0) {
  19.              facebook.setAccessExpires(expires);
  20.          }
  21.          /*
  22.           * Only call authorize if the access_token has expired.
  23.           */
  24.          if(!facebook.isSessionValid()) {
  25.                  Log.d("MyTag","In Authorize");
  26.                   facebook.authorize(this, new String[] {"publish_stream", "email"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
  27.                   public void onComplete(Bundle values) {
  28.                      
  29.                       shareAppPreferences.saveAccessToken(facebook.getAccessToken());
  30.                       Log.d("MyTag","face token: " +facebook.getAccessToken());
  31.                       shareAppPreferences.saveAccessExpires(facebook.getAccessExpires());
  32.                       completeLogin();
  33.                   }
  34.      
  35.                   public void onFacebookError(FacebookError e) {
  36.                     Toast.makeText(getBaseContext(), "onFacebookError", Toast.LENGTH_SHORT).show();
  37.                 }
  38.    
  39.                 public void onError(DialogError e) {
  40.                     Toast.makeText(getBaseContext(), "onError", Toast.LENGTH_SHORT).show();
  41.                 }
  42.                 public void onCancel() {
  43.                
  44.               }
  45.               });
  46.          }
  47.          else{
  48.                  completeLogin();
  49.          }
  50.                
  51.     }
  52.     private void completeLogin()
  53.     {
  54.         shareLinkOnFB();
  55.        
  56.         Intent intent = new Intent(this,MainActivity.class);
  57.         Log.d("MyTag", "starting activity");
  58.         startActivity(intent);
  59.     }
  60.    public void shareLinkOnFB(){
  61.         Facebook mFacebook = ((GlobalVars)getApplicationContext()).facebook;
  62.         AsyncFacebookRunner mAsyncRunner;
  63.         mAsyncRunner = new AsyncFacebookRunner(mFacebook);
  64.         Bundle params = new Bundle();
  65.  
  66.         params.putString("message", "Test");
  67.         params.putString("name", "ka");
  68.         params.putString("link", "http://www.facebook.com");
  69.         params.putString("description", "desc ...");
  70.  
  71.         mAsyncRunner.request("me/feed", params, new WallPostRequestListener());
  72.          
  73.         Toast.makeText(Login.this, "Posting to your Wall...", Toast.LENGTH_SHORT).show();      
  74.     }
  75.     public class WallPostRequestListener extends BaseRequestListener {
  76.  
  77.         public void onComplete(final String response, final Object state) {
  78.             Log.d("Facebook-Example", "Got response: " + response);
  79.             String message = "<empty>";
  80.             try {
  81.                 JSONObject json = Util.parseJson(response);
  82.                 message = json.getString("message");
  83.             } catch (JSONException e) {
  84.                 Log.w("Facebook-Example", "JSON Error in response");
  85.             } catch (FacebookError e) {
  86.                 Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
  87.             }
  88.             final String text = "Your Wall Post: " + message;
  89.         }
  90.     }
  91.     public abstract class BaseRequestListener implements RequestListener {
  92.  
  93.         public void onFacebookError(FacebookError e, final Object state) {
  94.             Log.e("Facebook", e.getMessage());
  95.             e.printStackTrace();
  96.         }
  97.  
  98.         public void onFileNotFoundException(FileNotFoundException e,
  99.                 final Object state) {
  100.             Log.e("Facebook", e.getMessage());
  101.             e.printStackTrace();
  102.         }
  103.  
  104.         public void onIOException(IOException e, final Object state) {
  105.             Log.e("Facebook", e.getMessage());
  106.             e.printStackTrace();
  107.         }
  108.  
  109.         public void onMalformedURLException(MalformedURLException e,
  110.                 final Object state) {
  111.             Log.e("Facebook", e.getMessage());
  112.             e.printStackTrace();
  113.         }
  114.     }
  115.  
  116.     @Override
  117.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  118.         super.onActivityResult(requestCode, resultCode, data);
  119.  
  120.         facebook.authorizeCallback(requestCode, resultCode, data);
  121.     }
clone this paste RAW Paste Data