Post on Facebook wall using Facebook Android SDK without opening dialog box

前端 未结 5 1723
抹茶落季
抹茶落季 2020-11-29 19:56

Using the Facebook SDK, I can login and store my access_token into a database. When I try to create a post, the Facebook wall is still empty on both my phone an

5条回答
  •  粉色の甜心
    2020-11-29 20:31

    This class helps me for sent messages on my Facebook wall WITHOUT dialog:

    public class FBManager{
    
      private static final String FB_ACCESS_TOKEN = "fb_access_token";
      private static final String FB_EXPIRES = "fb_expires";
    
      private Activity context;
      private Facebook facebookApi;
    
      private Runnable successRunnable=new Runnable(){
        @Override
        public void run() {
            Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
        }
      };    
    
      public FBManager(Activity context){
    
        this.context = context;
    
        facebookApi = new Facebook(FB_API_ID);
    
        facebookApi.setAccessToken(restoreAccessToken());
    
      } 
    
      public void postOnWall(final String text, final String link){
        new Thread(){
            @Override
            public void run(){
                try {
                    Bundle parameters = new Bundle();
                    parameters.putString("message", text);
                    if(link!=null){
                        parameters.putString("link", link);
                    }
                    String response = facebookApi.request("me/feed", parameters, "POST");
                    if(!response.equals("")){
                        if(!response.contains("error")){
                            context.runOnUiThread(successRunnable);                     
                        }else{
                            Log.e("Facebook error:", response);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();     
      }
    
    
      public void save(String access_token, long expires){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Editor editor=prefs.edit();
        editor.putString(FB_ACCESS_TOKEN, access_token);
        editor.putLong(FB_EXPIRES, expires);
        editor.commit();
      }
    
      public String restoreAccessToken(){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(FB_ACCESS_TOKEN, null);      
      }
    
      public long restoreExpires(){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getLong(FB_EXPIRES, 0);        
      } 
    

    }

提交回复
热议问题