简体   繁体   English

Android Facebook集成-预定义的墙贴

[英]Android Facebook Integration - Predefined Wall Post

I want to add functionality to my app where a user can share with their friends that they are using my app. 我想向我的应用程序添加功能,以便用户可以与他们的朋友分享他们正在使用我的应用程序。 I want the post to have a predefined message, but all I can get working is a regular post from my app. 我希望该帖子具有预定义的消息,但是我能做的只是来自我的应用的常规帖子。 Is there anyway I can do this and maybe with an image as well. 无论如何,我可以做到这一点,也许还有图像。 but that's not as important.. Any ideas? 但这不是那么重要。有什么想法吗?

Here's where I handle everything with Facebook (Logging in AND Posting): 这是我使用Facebook处理一切的地方(登录和发布):

public class FacebookSSO extends Activity {

Facebook facebook = new Facebook("APP_ID");
private SharedPreferences mPrefs;
private static final String TAG = "FacebookConnect";
private static final String MSG = "Signed In Using MNWV For Android - Check It Out!";    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*
     * Get existing access_token if any
     */
    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);
    }
    if(expires != 0) {
        facebook.setAccessExpires(expires);
    }

    /*
     * Only call authorize if the access_token has expired.
     */
    if(!facebook.isSessionValid()) {

            facebook.dialog(this, "oauth", new DialogListener() {
          @Override
          public void onComplete(Bundle values) {  
              SharedPreferences.Editor editor = mPrefs.edit();
              editor.putString("access_token", facebook.getAccessToken());
              editor.putLong("access_expires", facebook.getAccessExpires());
              editor.commit();                       
              Bundle parameters = new Bundle();
              parameters.putString(TAG, MSG);// the message to post to the wall                    

              facebook.dialog(FacebookSSO.this, "stream.publish", parameters, new DialogListener() { 
                  @Override
                public void onComplete(Bundle values) {  
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires", facebook.getAccessExpires());
                    editor.commit();  

                       Intent in = new Intent(FacebookSSO.this, MNWVMainScreen.class);
                         startActivity(in);
                }

                @Override
                public void onFacebookError(FacebookError error) {}

                @Override
                public void onError(DialogError e) {}

                @Override
                public void onCancel() {}

              });
          }

          @Override
          public void onFacebookError(FacebookError error) {}

          @Override
          public void onError(DialogError e) {}

          @Override
          public void onCancel() {}
      });

            }
} 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}
}

Suppose that you want to send message to Facebook on button click. 假设您要在单击按钮时向Facebook发送消息。

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    Button sharebutton = (Button) findViewById(R.id.share_button);
    sharebutton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (facebook.isSessionValid()) {
                postFacebookMessage();
            } else {
                facebook.authorize(YourActivity.this, new String[] {"publish_stream"}, new FacebookAuthListener() {
                    @Override
                    public void onComplete(Bundle values) {
                        postFacebookMessage();
                    }
                });
            }
        }
    });

    ...
}

And put this method in your activity: 并将此方法放入您的活动中:

private void postFacebookMessage() {
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    Bundle params = new Bundle();
    params.putString("message", "I am using great App!");
    params.putString("picture", "http://mysite.com/logo.jpg");
    mAsyncRunner.request("me/feed", params, "POST", new FacebookPostListener(), null);
}

And add this class: 并添加此类:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.util.Log;

import com.facebook.android.FacebookError;
import com.facebook.android.AsyncFacebookRunner.RequestListener;

public class FacebookPostListener implements RequestListener {
    private static final String TAG = "FacebookPostListener";

    @Override
    public void onComplete(final String response, final Object state) {
        Log.d(TAG, "Facebook published the post. Got response: " + response);
    }

    @Override
    public void onFacebookError(FacebookError e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onIOException(IOException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onMalformedURLException(MalformedURLException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }
}

And this: 和这个:

import android.os.Bundle;
import android.util.Log;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class FacebookAuthListener implements DialogListener {

    private static final String TAG = FacebookAuthListener.class.getSimpleName();

    @Override
    public void onFacebookError(FacebookError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onError(DialogError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onComplete(Bundle values) {
    }

    @Override
    public void onCancel() {
        // Do nothing
    }
}

I think that's all :) 我认为就这样:)

I simply use: 我只是使用:

  public void inviteSomeone(View view) {

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");

    Intent chooser = Intent.createChooser(intent, "Pick your app");

    String shareSub = "This App is cool!";
    intent.putExtra(Intent.EXTRA_TEXT, shareSub);

  }

And for the button in the XML: 对于XML中的按钮:

<!--for the Invite button-->
<Button
    android:id="@+id/btnInvite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:onClick="inviteSomeone"
    android:text="@string/btnInvite" />

It will work for WhatsApp, text messages, Facebook wall posts etc...in this case with the predefined text: "This App is cool!" 它将适用于WhatsApp,短信,Facebook墙贴等,在这种情况下使用预定义的文本:“此应用非常酷!”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM