如何从我的Android应用程序发布到Facebook墙?

时间:2015-04-11 06:12:14

标签: android facebook facebook-graph-api

我已将Facebook与我的应用程序集成。但我的要求是在获得Facebook帐户访问权限后,我的应用程序应自动提取一些文字,图片,以下链接like this

到我的脸墙,然后重定向到另一个活动(例如 - SecondAcitivity)

FacebookActivity.java

package facebookIntegration;


import com.pcs.sliderringtineproj.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;



public class FacebookActivity extends FragmentActivity {

private MainFragment mainFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, mainFragment)
        .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
        .findFragmentById(android.R.id.content);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

MainFragment.java

package facebookIntegration;

import java.util.Arrays;

import com.facebook.SessionState;
import com.facebook.Session;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;
import com.pcs.sliderringtineproj.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;



public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper; 

private Session.StatusCallback callback=new Session.StatusCallback(){
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }

};  

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_facebook, container, false);
    LoginButton authButton = (LoginButton)    view.findViewById(R.id.authButton);

    if(Session.getActiveSession().isOpened()){
        Toast.makeText(getActivity(), "logged in to FB", 1).show();
    }else{
        authButton.setFragment(this);
        authButton.setReadPermissions(Arrays.asList("public_profile"));
    }
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

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

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

}

1 个答案:

答案 0 :(得分:0)

Bundle params = new Bundle(); params.putString(“message”,message);

String uri =“/ me / feed?message =”+ message +“”+“& access_token =”+ session.getAccessToken();

uri = uri.replace(“”,“%20”);

新请求(

        session,
        uri,
        params,
        HttpMethod.POST,
        new Request.Callback() {
               public void onCompleted(Response response) {
                                                 Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                            ).executeAsync();