代表用户从Android游戏在Facebook上自动发布

时间:2015-02-05 07:38:41

标签: android facebook share

我使用Processing开发了基于物理的(Box2d)Android游戏,我想为它添加分数分享选项,以便用户可以在游戏后的Facebook时间线上分享他/她的最佳分数。我在eclipse中设置了Facebook SDK。我通过互联网搜索并找到了这个解决方案:

public class FacebookConnector {
        private static final String APP_ID = "*************";
        private Facebook facebook;
        private AsyncFacebookRunner mAsyncRunner;
        String FILENAME = "AndroidSSO_data";
        SharedPreferences mPrefs;

        public FacebookConnector() {
            facebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(facebook);
        }

        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);
            }

            if (expires != 0) {
                facebook.setAccessExpires(expires);
            }

            if (!facebook.isSessionValid()) {
                facebook.authorize(LocalPakistaniGames.this,
                        new String[] { "email",
                        "publish_stream" }, new DialogListener() {

                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    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();
                    }

                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
            }
        }

        public void logoutFromFacebook() {
            mAsyncRunner.logout(LocalPakistaniGames.this,
                    new RequestListener() {
                @Override
                public void onComplete(String response, Object state) {
                    Log.d("Logout from Facebook", response);
                    if (Boolean.parseBoolean(response) == true) {
                        // User successfully Logged out
                    }
                }

                @Override
                public void onIOException(IOException e, Object state) {
                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e,
                        Object state) {
                }

                @Override
                public void onMalformedURLException(MalformedURLException e,
                        Object state) {
                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                }
            });
        }

        public void getProfileInformation() {
            mAsyncRunner.request("me", new RequestListener() {
                public void onComplete(String response, Object state) {
                    Log.d("Profile", response);
                    String json = response;
                    try {
                        JSONObject profile = new JSONObject(json);
                        // getting name of the user
                        final String name = profile.getString("name");
                        // getting email of the user
                        final String email = profile.getString("email");

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "Name: " + name + "\nEmail: " + email,
                                        Toast.LENGTH_LONG).show();
                            }

                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                public void onIOException1(IOException e, Object state) {
                }

                public void onFileNotFoundException1(FileNotFoundException e,
                        Object state) {
                }

                public void onMalformedURLException1(MalformedURLException e,
                        Object state) {
                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                }

                @Override
                public void onIOException(IOException e, Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onMalformedURLException(MalformedURLException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }
            });
        }

        public void postToWall(int level, int score) {
            // post on user's wall.
            String msg = "I just made new best score in Level " + level
                    + ". My new Best Score is " + score + ". Beat my score!";
            final Bundle parameters = new Bundle();
            parameters.putString("description", msg);
            parameters.putString("picture", "http://i57.tinypic.com/fui2o.png");
            parameters.putString("link",
                    "https://www.facebook.com/LocalPakistaniGamesAndroid");
            parameters.putString("name", "Local Pakistani Games");
            parameters.putString("caption",
                    "Share this. Be a part of preserving Pakistani culture.");
            LocalPakistaniGames.this.runOnUiThread(new Runnable() {
                public void run() {
                    facebook.dialog(LocalPakistaniGames.this, "feed",
                            parameters, new DialogListener() {

                                @Override
                                public void onComplete(Bundle values) {
                                    // TODO Auto-generated method stub
                                    if (values != null) {
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Shared successfully on your timeline!",
                                            Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(
                                                LocalPakistaniGames.this,
                                                "Share cancelled!",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onFacebookError(FacebookError e) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Facebook Error!",
                                            Toast.LENGTH_SHORT)
                                            .show();
                                }

                                @Override
                                public void onError(DialogError e) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Error!", Toast.LENGTH_SHORT)
                                            .show();
                                }

                                @Override
                                public void onCancel() {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Share cancelled!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                }
            });
        }
    }

它工作正常,并提供一个预先填充的对话框,用户可以在其中共享或关闭对话框。我已经检查过,它在Facebook时间线上正确分享。但问题是它没有在设备上安装Facebook应用程序。它在我的设备上使用chrome登录Facebook。 有没有办法强迫它使用Facebook应用程序的Android而不是去Chrome(或任何其他浏览器)??

2 个答案:

答案 0 :(得分:0)

使用Facebook SDK存在许多问题,但我会直接解决您的问题。

您正在使用" Feed"对话框分享。这是一个Web对话框,这就是它弹出WebView(而不是实际的浏览器应用程序)的原因。您也不会将任何会话或访问令牌传递给Feed对话框,这就是用户需要先登录才能共享的原因。

如果您想使用Facebook应用分享,我建议您使用此文档:https://developers.facebook.com/docs/android/share

如果您想正确使用Facebook SDK(而不是旧的弃用版),请从此处开始:https://developers.facebook.com/docs/android/getting-started/

答案 1 :(得分:0)

Here是您问题的解决方案。如果你想在Facebook墙上制作自动出版物,你必须使用图形API。 此外,您要做的第一件事是使用Facebook登录,之后,您必须再次请求权限。 如果您使用LoginManager

LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));

此代码行为您的应用提供了代表用户制作出版物的权限