Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bundle params = new Bundle();
- //params.putString("message", "I want to post this text only.");
- params.putString("name", "Facebook SDK for Android");
- params.putString("caption", "Build great social apps and get more installs.");
- params.putString("description", "Description");
- params.putString("link", "https://developers.facebook.com/android");
- params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
- WebDialog feedDialog = (
- new WebDialog.FeedDialogBuilder(getActivity(),
- Session.getActiveSession(),
- params))
- .setOnCompleteListener(new OnCompleteListener() {
- if (facebook.getAccessToken() != null) {
- postOnWall("msg to share");
- } else {
- loginToFacebook();
- }
- public void postOnWall(String msg) {
- Log.d("Tests", "Testing graph API wall post");
- try {
- String response = facebook.request("me");
- Bundle parameters = new Bundle();
- parameters.putString("message", msg);
- parameters.putString("description", "test test test");
- // parameters.putByteArray("message", msg);
- response = facebook.request("me/feed", parameters, "POST");
- Log.d("Tests", "got response: " + response);
- if (response == null || response.equals("")
- || response.equals("false")) {
- Log.v("Error", "Blank response");
- Toast.makeText(getApplicationContext(), "FacebookError",Toast.LENGTH_SHORT).show();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void loginToFacebook() {
- mPrefs = getPreferences(MODE_PRIVATE);
- String access_token = mPrefs.getString("access_token", null);
- long expires = mPrefs.getLong("access_expires", 0);
- if (access_token != null) {
- facebook.setAccessToken(access_token);
- Log.d("FB Sessions", "" + facebook.isSessionValid());
- }
- if (expires != 0) {
- facebook.setAccessExpires(expires);
- }
- if (!facebook.isSessionValid()) {
- facebook.authorize(this,
- new String[] { "email", "publish_stream" },
- new DialogListener() {
- @Override
- public void onCancel() {
- // Function to handle cancel event
- }
- @Override
- public void onComplete(Bundle values) {
- // Function to handle complete event
- // Edit Preferences and update facebook acess_token
- SharedPreferences.Editor editor = mPrefs.edit();
- editor.putString("access_token",
- facebook.getAccessToken());
- editor.putLong("access_expires",
- facebook.getAccessExpires());
- editor.commit();
- Toast.makeText(sms_by_id.this,
- "Successfully Login", Toast.LENGTH_LONG)
- .show();
- pwindo.dismiss();
- // Making Login button invisible
- }
- @Override
- public void onError(DialogError error) {
- // Function to handle error
- }
- @Override
- public void onFacebookError(FacebookError fberror) {
- // Function to handle Facebook errors
- pwindo.dismiss();
- Toast.makeText(sms_by_id.this, "Facebook Error",
- Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- public class ShareOnFacebook extends Activity{
- private static final String APP_ID = "12dfgdfgf";
- private static final String[] PERMISSIONS = new String[] {"publish_stream"};
- private static final String TOKEN = "access_token";
- private static final String EXPIRES = "2040";
- private static final String KEY = "gfdfdfdfdfd;
- private Facebook facebook;
- private String messageToPost;
- public boolean saveCredentials(Facebook facebook) {
- Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
- editor.putString(TOKEN, facebook.getAccessToken());
- editor.putLong(EXPIRES, facebook.getAccessExpires());
- return editor.commit();
- }
- public boolean restoreCredentials(Facebook facebook) {
- SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
- facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
- facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
- return facebook.isSessionValid();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- facebook = new Facebook(APP_ID);
- restoreCredentials(facebook);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.facebook_dialog);
- facebook = new Facebook(APP_ID);
- restoreCredentials(facebook);
- String facebookMessage = getIntent().getStringExtra("facebookMessage");
- if (facebookMessage == null){
- facebookMessage = "Test wall post";
- }
- messageToPost = facebookMessage;
- }
- public void doNotShare(View button){
- finish();
- }
- public void share(View button){
- if (! facebook.isSessionValid()) {
- loginAndPostToWall();
- }
- else {
- postToWall(messageToPost);
- /*Intent backhome = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
- startActivity(backhome);*/
- }
- }
- public void loginAndPostToWall(){
- facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
- }
- public void postToWall(String message){
- Bundle parameters = new Bundle();
- parameters.putString("message", message);
- // parameters.putString("link", "http://theaterbalcony.com/tag/torpedo-3d-malayalam-cartoon/");
- 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")) {
- showToast("Blank response.");
- }
- else {
- showToast("Message posted to your facebook wall!");
- }
- finish();
- } catch (Exception e) {
- showToast("Failed to post to wall!");
- e.printStackTrace();
- finish();
- }
- }
- class LoginDialogListener implements DialogListener {
- public void onComplete(Bundle values) {
- saveCredentials(facebook);
- if (messageToPost != null){
- postToWall(messageToPost);
- }
- }
- public void onFacebookError(FacebookError error) {
- showToast("Authentication with Facebook failed!");
- finish();
- }
- public void onError(DialogError error) {
- showToast("Authentication with Facebook failed!");
- finish();
- }
- public void onCancel() {
- showToast("Authentication with Facebook cancelled!");
- finish();
- }
- }
- private void showToast(String message){
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- finish();
- }
- @Override
- public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
- // if menu button is pressed
- if (pKeyCode == KeyEvent.KEYCODE_MENU
- && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
- Intent home = new Intent(ShareOnFacebook.this, Quotes_Tab.class);
- finish();
- startActivity(home);
- } else if (pKeyCode == KeyEvent.KEYCODE_BACK
- && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
- Intent home = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
- finish();
- startActivity(home);
- return super.onKeyDown(pKeyCode, pEvent);
- }
- return super.onKeyDown(pKeyCode, pEvent);
- }
- @Override
- protected void onStop() {
- // TODO Auto-generated method stub
- super.onStop();
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement