0

我正在开发一个使用 3rd 方应用程序 Facebook 的黑莓应用程序。

现在,我的问题是,当我第一次发布消息时,我将被重定向到 Facebook 登录屏幕并发布消息。但是一旦我从我的应用程序注销并再次登录以将消息发布到 facebook,它会直接发布消息而不询问凭证信息。

登录facebook的以下代码:

    public class FacebookMain implements ActionListener{// extends MainScreen implements ActionListener {

        // Constants
        public final static String NEXT_URL = "http://www.facebook.com/connect/login_success.html";
        public final static String APPLICATION_ID ="261890490596037"; 
        private final static long persistentObjectId = 0x854d1b7fa43e3577L;
        //APPLICATION SECRET ID="f7e096af696ef81e72268f49a6381a8d"

        static final String ACTION_ENTER = "updateStatus";
        static final String ACTION_SUCCESS = "statusUpdated";
        static final String ACTION_ERROR = "error";
        private ActionScreen actionScreen;

        private PersistentObject store;

        private LoginScreen loginScreen;
        private LogoutScreen logoutScreen;
        private HomeScreen homeScreen;
        private UpdateStatusScreen updateStatusScreen;
        private RecentUpdatesScreen recentUpdatesScreen;
        private UploadPhotoScreen uploadPhotoScreen;
        private FriendsListScreen friendsListScreen;
        private PokeFriendScreen pokeFriendScreen;
        private PostWallScreen postWallScreen;
        private SendMessageScreen sendMessageScreen;

        private String postMessage;

        private FacebookContext fbc;
        public static boolean isWallPosted=false;

        public FacebookMain(String postMessge) {
            this.postMessage=postMessge;

            checkPermissions();
            init();

            if ((fbc != null) && fbc.hasValidAccessToken()) {

                /*homeScreen = new HomeScreen(fbc);
                homeScreen.addActionListener(this);
                UiApplication.getUiApplication().pushScreen(homeScreen);*/

                try {
                    new FBUser("me", fbc.getAccessToken()).setStatus(postMessage);
                    actionScreen=new ActionScreen();
                    actionScreen.fireAction(ACTION_SUCCESS);
                    Dialog.alert("Wall Posted ");
                    isWallPosted=true;
                    //UiApplication.getUiApplication().popScreen(loginScreen);
                } catch (Exception e) {
                    actionScreen.fireAction(ACTION_ERROR, e.getMessage());
                    //UiApplication.getUiApplication().popScreen(loginScreen);
                }

            } else {

                loginScreen = new LoginScreen(fbc,postMessge);
                loginScreen.addActionListener(this);
                UiApplication.getUiApplication().pushScreen(loginScreen);
            }
        }

        private void init() {
            store = PersistentStore.getPersistentObject(persistentObjectId);
            synchronized (store) {
                if (store.getContents() == null) {
                    store.setContents(new FacebookContext(NEXT_URL, APPLICATION_ID));
                    store.commit();
                }
            }
            fbc = (FacebookContext) store.getContents();
        }

        private void checkPermissions() {

            ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();
            ApplicationPermissions original = apm.getApplicationPermissions();

            if ((original.getPermission(ApplicationPermissions.PERMISSION_INPUT_SIMULATION) == ApplicationPermissions.VALUE_ALLOW) && (original.getPermission(ApplicationPermissions.PERMISSION_DEVICE_SETTINGS) == ApplicationPermissions.VALUE_ALLOW) && (original.getPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION) == ApplicationPermissions.VALUE_ALLOW) && (original.getPermission(ApplicationPermissions.PERMISSION_INTERNET) == ApplicationPermissions.VALUE_ALLOW) && (original.getPermission(ApplicationPermissions.PERMISSION_SERVER_NETWORK) == ApplicationPermissions.VALUE_ALLOW) && (original.getPermission(ApplicationPermissions.PERMISSION_EMAIL) == ApplicationPermissions.VALUE_ALLOW)) {
                return;
            }

            ApplicationPermissions permRequest = new ApplicationPermissions();
            permRequest.addPermission(ApplicationPermissions.PERMISSION_INPUT_SIMULATION);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_DEVICE_SETTINGS);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_INTERNET);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_SERVER_NETWORK);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_EMAIL);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_INTERNET);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_AUTHENTICATOR_API);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_SERVER_NETWORK);
            permRequest.addPermission(ApplicationPermissions.PERMISSION_WIFI);

            boolean acceptance = ApplicationPermissionsManager.getInstance().invokePermissionsRequest(permRequest);

            if (acceptance) {
                // User has accepted all of the permissions.
                return;
            } else {
            }
        }

        public void saveContext(FacebookContext pfbc) {
            synchronized (store) {
                store.setContents(pfbc);
                System.out.println(pfbc);
                store.commit();
            }
        }

        public void logoutAndExit() {
            saveContext(null);
            logoutScreen = new LogoutScreen(fbc);
            logoutScreen.addActionListener(this);
        }

        public void saveAndExit() {
            saveContext(fbc);
            exit();
        }

        private void exit() {
            AppenderFactory.close();
            System.exit(0);
        }

        public void onAction(Action event) {/*

            if (event.getSource() == loginScreen) {
                if (event.getAction().equals(LoginScreen.ACTION_LOGGED_IN)) {
                    try {
                        fbc.setAccessToken((String) event.getData());
                        try {
                            new FBUser("me", fbc.getAccessToken()).setStatus(postMessage);
                            actionScreen=new ActionScreen();
                            actionScreen.fireAction(ACTION_SUCCESS);
                            Dialog.alert("Wall Posted ");
                        } catch (Exception e) {
                            actionScreen.fireAction(ACTION_ERROR, e.getMessage());
                        }
                        try {
                            if (homeScreen == null) {
                                homeScreen = new HomeScreen(fbc);
                                homeScreen.addActionListener(this);
                            }
                            UiApplication.getUiApplication().pushScreen(homeScreen);

                        } catch (Exception e) {
                            e.printStackTrace();
                            Dialog.alert("Error: " + e.getMessage());
                        }

                    } catch (Throwable t) {
                        t.printStackTrace();
                        Dialog.alert("Error: " + t.getMessage());
                    }

                } else if (event.getAction().equals(LoginScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }

            } else if (event.getSource() == logoutScreen) {
                if (event.getAction().equals(LogoutScreen.ACTION_LOGGED_OUT)) {
                    exit();
                }

            } else if (event.getSource() == homeScreen) {
                if (event.getAction().equals(UpdateStatusScreen.ACTION_ENTER)) {
                    if (updateStatusScreen == null) {
                        updateStatusScreen = new UpdateStatusScreen(fbc);
                        updateStatusScreen.addActionListener(this);
                    }
                    UiApplication.getUiApplication().pushScreen(updateStatusScreen);
                    try {
                        new FBUser("me", fbc.getAccessToken()).setStatus("");
                        actionScreen=new ActionScreen();
                        actionScreen.fireAction(ACTION_SUCCESS);
                    } catch (Exception e) {
                        actionScreen.fireAction(ACTION_ERROR, e.getMessage());
                    }

                } else if (event.getAction().equals(RecentUpdatesScreen.ACTION_ENTER)) {
                    if (recentUpdatesScreen == null) {
                        recentUpdatesScreen = new RecentUpdatesScreen(fbc);
                        recentUpdatesScreen.addActionListener(this);
                    }
                    recentUpdatesScreen.loadList();
                    UiApplication.getUiApplication().pushScreen(recentUpdatesScreen);

                } else if (event.getAction().equals(UploadPhotoScreen.ACTION_ENTER)) {
                    if (uploadPhotoScreen == null) {
                        uploadPhotoScreen = new UploadPhotoScreen(fbc);
                        uploadPhotoScreen.addActionListener(this);
                    }
                    UiApplication.getUiApplication().pushScreen(uploadPhotoScreen);

                } else if (event.getAction().equals(FriendsListScreen.ACTION_ENTER)) {
                    if (friendsListScreen == null) {
                        friendsListScreen = new FriendsListScreen(fbc);
                        friendsListScreen.addActionListener(this);
                    }
                    friendsListScreen.loadList();
                    UiApplication.getUiApplication().pushScreen(friendsListScreen);

                } else if (event.getAction().equals(PokeFriendScreen.ACTION_ENTER)) {
                    if (pokeFriendScreen == null) {
                        pokeFriendScreen = new PokeFriendScreen(fbc);
                        pokeFriendScreen.addActionListener(this);
                    }
                    UiApplication.getUiApplication().pushScreen(pokeFriendScreen);

                } else if (event.getAction().equals(PostWallScreen.ACTION_ENTER)) {
                    if (postWallScreen == null) {
                        postWallScreen = new PostWallScreen(fbc);
                        postWallScreen.addActionListener(this);
                    }
                    postWallScreen.loadList();
                    UiApplication.getUiApplication().pushScreen(postWallScreen);

                } else if (event.getAction().equals(SendMessageScreen.ACTION_ENTER)) {
                    if (sendMessageScreen == null) {
                        sendMessageScreen = new SendMessageScreen(fbc);
                        sendMessageScreen.addActionListener(this);
                    }
                    UiApplication.getUiApplication().pushScreen(sendMessageScreen);
                }

            } else if (event.getSource() == updateStatusScreen) {
                if (event.getAction().equals(UpdateStatusScreen.ACTION_SUCCESS)) {
                    Dialog.inform("Status updated");

                    try {
                        UiApplication.getUiApplication().popScreen(updateStatusScreen);
                    } catch (IllegalArgumentException e) {
                    }

                } else if (event.getAction().equals(UpdateStatusScreen.ACTION_SUCCESS)) {
                    Dialog.alert("Error: " + event.getData());
                }

            } else if (event.getSource() == recentUpdatesScreen) {
                if (event.getAction().equals(RecentUpdatesScreen.ACTION_SUCCESS)) {
                    try {
                        UiApplication.getUiApplication().popScreen(recentUpdatesScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(RecentUpdatesScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            } else if (event.getSource() == uploadPhotoScreen) {
                if (event.getAction().equals(UploadPhotoScreen.ACTION_SUCCESS)) {
                    try {
                        UiApplication.getUiApplication().popScreen(uploadPhotoScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(UploadPhotoScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            } else if (event.getSource() == friendsListScreen) {
                if (event.getAction().equals(FriendsListScreen.ACTION_SUCCESS)) {
                    try {
                        UiApplication.getUiApplication().popScreen(friendsListScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(FriendsListScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            } else if (event.getSource() == pokeFriendScreen) {
                if (event.getAction().equals(PokeFriendScreen.ACTION_SUCCESS)) {
                    try {
                        UiApplication.getUiApplication().popScreen(pokeFriendScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(PokeFriendScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            } else if (event.getSource() == postWallScreen) {
                if (event.getAction().equals(PostWallScreen.ACTION_SUCCESS)) {
                    Dialog.inform("Wall posted");

                    try {
                        UiApplication.getUiApplication().popScreen(postWallScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(PostWallScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            } else if (event.getSource() == sendMessageScreen) {
                if (event.getAction().equals(SendMessageScreen.ACTION_SUCCESS)) {
                    try {
                        UiApplication.getUiApplication().popScreen(sendMessageScreen);
                    } catch (IllegalArgumentException e) {
                    }
                } else if (event.getAction().equals(SendMessageScreen.ACTION_ERROR)) {
                    Dialog.alert("Error: " + event.getData());
                }
            }
         */}
    }

和下面的代码来清除凭证

public void logoutAndExit() {
        saveContext(null);
        logoutScreen = new LogoutScreen(fbc);
        logoutScreen.addActionListener(this);
    }
4

0 回答 0