Facebook SDK 3.0:如何删除“您已经授权此应用程序”屏幕

时间:2013-07-12 13:17:55

标签: android facebook facebook-graph-api oauth

我在我的Android应用程序中使用Facebook,它运行得很好。

然而,我意识到当我每次使用“确定”和“取消”选项说“您已经授权使用此应用程序”时,会出现无用的屏幕。

如何从我的应用中避开或删除此屏幕。

我在谷歌上搜索过,我找到了旧版Facebook SDK的答案但是如何使用新的Facebook SDK(3.0及以上版本)实现它?

这是我的代码,

public void onClick(View v)
        {

            Bundle myParams = new Bundle();
            myParams.putString("message", " DOWNLOAD THE 'Saragama' ANDROID APP FROM " + url_android.toString()
                    + ". Fully integrated with Facebook and Twitter.");

            myParams.putString("name", mSharingText.getText().toString());
            myParams.putString("caption", "www.saragama.com");
            myParams.putString("description", "Various religios music, chants, mantras, christian songs, islamic music, quran and more on saragama");
            myParams.putString("link", "http://android.saragama.com");
            myParams.putString("picture", albumImageURL);

            if (PlusUtilities.isInternetConnected())
                {
                    // ~post your data
                    Session session = Session.getActiveSession();
                    if (session != null)
                        {
                            MyLog.w("session state", session.getState().toString());
                            if (session.getState().equals(SessionState.OPENED) || session.getState().equals(SessionState.OPENED_TOKEN_UPDATED))
                                {
                                    publishStory(myParams);
                                }
                            else
                                {
                                    plusUtilities1.showAlertDialog("Please login first!");
                                }
                        }
                    else
                        {
                            plusUtilities1.showAlertDialog("Please login first!");
                        }

                }
            else
                {
                    plusUtilities1.showAlertDialog("Problem occured with your internet connection!");
                }
        }

    // Facebook Login-------------------------------------------------------------------------------------
    private void loginToFacebook()
        {
            if (PlusUtilities.isInternetConnected())
                {
                    Session session = Session.getActiveSession();

                    // if session is in exceptional state
                    if (session.getState() == SessionState.CLOSED_LOGIN_FAILED || session.getState() == SessionState.OPENING)
                        {
                            session.closeAndClearTokenInformation();
                        }

                    if (!session.isOpened() && !session.isClosed())
                        {
                            String[] PERMISSION_ARRAY_PUBLISH =
                                {"publish_actions" };
                            List<String> PERMISSION_LIST = Arrays.asList(PERMISSION_ARRAY_PUBLISH);
                            session.openForPublish(new Session.OpenRequest(FacebookActivity.this).setPermissions(PERMISSION_LIST).setCallback(
                                    statusCallback1));
                        }
                    else
                        {
                            Session.openActiveSession(this, true, statusCallback1);
                        }
                }
            else
                {
                    plusUtilities1.showAlertDialog("Please check your internet connection!");
                }
        }

    @Override
    protected void onSaveInstanceState(Bundle outState)
        {
            super.onSaveInstanceState(outState);
            Session session = Session.getActiveSession();
            Session.saveSession(session, outState);
        }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent data)
        {

            super.onActivityResult(requestCode, responseCode, data);

            // plusUtilities1.ShowToast("onActivityResult");
            MyLog.i("onActivityResult requestCode :responsecode:session state", requestCode + ":" + responseCode + ":"
                    + Session.getActiveSession().getState().toString());
            switch (requestCode)
                {

                case MyRaagaLoginActivity.REQUEST_CODE_FACEBOOK_LOGIN:
                    if (responseCode == RESULT_OK)
                        {
                                    MyLog.e("Login", "trying to facebook Login");
                                    Session.getActiveSession().onActivityResult(Act1, requestCode, responseCode, data);

                        }
                    else
                        {
                            plusUtilities1.ShowToast("User access denied!");
                        }
                    break;

                default:
                    MyLog.i("case:", "default");
                    break;
                }

        }

    /**
     * function to get active facebook session if already exist or create the new session
     * 
     * @param savedInstanceState
     *            =check if Session exist and restored in bundle during onSaveInstanceState() system call
     * @author DeepakD
     */
    public void GetOrCreateFacebookActiveSession(Bundle savedInstanceState)
        {
            Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
            Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);

            Session session = Session.getActiveSession();

            // if session is null try to find if session is saved previously
            if (session == null)
                {
                    if (savedInstanceState != null)
                        {
                            session = Session.restoreSession(this, null, statusCallback1, savedInstanceState);
                        }

                    // if still session is null then create the new session
                    if (session == null)
                        {
                            session = new Session(this);
                        }

                    // set the created session as active session
                    Session.setActiveSession(session);

                }
            updateFBbutton();
        }

    /**
     * set login or logout button
     */
    private void updateFBbutton()
        {

            Session session = Session.getActiveSession();
            if (session.isOpened())
                {
                    FbLoginbtn.setText("FBLogout");
                    FbLoginbtn.setOnClickListener(new OnClickListener()
                        {
                            public void onClick(View view)
                                {
                                    onClickFBLogout();
                                }
                        });
                }
            else
                {
                    FbLoginbtn.setText("FBLogin");
                    FbLoginbtn.setOnClickListener(new OnClickListener()
                        {
                            public void onClick(View view)
                                {
                                    loginToFacebook();
                                }
                        });
                }

        }

    /**
     * Logout from facebook
     */
    private void onClickFBLogout()
        {
            Session session = Session.getActiveSession();
            if (!session.isClosed())
                {
                    session.closeAndClearTokenInformation();
                    plusUtilities1.ShowToast("Logout successful");
                }
        }

    /**
     * This class is used to fetch the current status of active session
     * 
     * @author DeepakD
     * 
     */
    private class SessionStatusCallback1 implements Session.StatusCallback
        {
            @Override
            public void call(Session session, SessionState state, Exception exception)
                {

                    updateFBbutton();
                    plusUtilities1.DissmissPD();
                    MyLog.w("session state", session.getState().toString());

                    if(session.getState()==SessionState.CLOSED_LOGIN_FAILED)
                        {
                            //login failed
                            plusUtilities1.ShowToast("Unable to connect,please try later..");
                            session.closeAndClearTokenInformation();
                        }

                    if (session.getState()== SessionState.OPENED)
                        {
                            //login successful
                            plusUtilities1.ShowToast("Login successful");
                        }
                }

        }


    /**
     * actually post the data on facebook
     * 
     * @param myParams
     *            Bundle of parameters to be post
     */
    private void publishStory(Bundle myParams)
        {
            plusUtilities1.ShowPD("Sharing data...");
            Session session = Session.getActiveSession();

            if (session != null)
                {

                    // Check for publish permissions
                    List<String> permissions = session.getPermissions();
                    try
                        {
                            if (!isSubsetOf(PERMISSIONS, permissions))
                                {
                                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
                                    session.requestNewPublishPermissions(newPermissionsRequest);
                                }
                        }
                    catch (Exception e1)
                        {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                    Request.Callback callback = new Request.Callback()
                        {
                            public void onCompleted(Response response)
                                {
                                    plusUtilities1.DissmissPD();

                                    MyLog.w("post response", "" + response.toString());
                                    if (response.getError() == null)
                                        {
                                            JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                                            String postId = null;
                                            try
                                                {
                                                    postId = graphResponse.getString("id");
                                                }
                                            catch (JSONException e)
                                                {

                                                    Toast.makeText(FacebookActivity.this, "JSON error " + e.getMessage(), Toast.LENGTH_LONG)
                                                            .show();

                                                }
                                            catch (Exception e)
                                                {
                                                    e.printStackTrace();
                                                    Toast.makeText(FacebookActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                                }
                                            FacebookRequestError error = response.getError();
                                            Log.e("post response", response.toString());
                                            if (error != null)
                                                {
                                                    Toast.makeText(FacebookActivity.this, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                                                }
                                            else
                                                {
                                                    Toast.makeText(FacebookActivity.this, "Posted successfully!",
                                                            Toast.LENGTH_LONG).show();
                                                    FlurryAgent.logEvent("Audio Facebook share -" + EventName);
                                                }
                                        }
                                    else
                                        {
                                            plusUtilities1.showAlertDialog("Something went wrong while posting!");
                                        }
                                }
                        };

                    MyLog.w("BUNDLE TO BE POSTED;", myParams.toString());
                    Request request = new Request(session, "/me/feed", myParams, HttpMethod.POST, callback);
                    RequestAsyncTask task = new RequestAsyncTask(request);
                    task.execute();
                }

        }

    public static boolean isSubsetOf(Collection<String> subset, Collection<String> superset)
        {
            for (String string : subset)
                {
                    if (!superset.contains(string))
                        {
                            return false;
                        }
                }
            return true;
        }

    protected void onStart()
        {

            super.onStart();
            MyLog.i("FacebookActivity", "onStart");
            FlurryAgent.onStartSession(this, KeysCls.Flurry_Analytics_Key);
            FlurryAds.displayAd(this, "AppCircle_Ads", linlayAdLayout);

            if (PlusUtilities.isInternetConnected())
                {
                    Session.getActiveSession().addCallback(statusCallback1);

                }
            else
                {
                    plusUtilities1.showAlertDialog("Please check your internet connection and try again!");
                }
        }

    /**
     * convert post's simple text to text with links
     * 
     * @param Name
     * @param Link
     * @return
     */
    String wallpostWithLink(String Name, String Link)
        {

            jo = new JSONObject();
            try
                {

                    jo.put("name", Name);
                    jo.put("link", Link);
                }
            catch (Exception e)
                {
                    e.printStackTrace();
                }

            return jo.toString();
        }

    @Override
    protected void onStop()
        {
            super.onStop();
            FlurryAgent.onEndSession(this);
        }

2 个答案:

答案 0 :(得分:1)

转到developers.facebook.comSettings-> Basic->Single Sign On -> YES

答案 1 :(得分:0)

在facebook sdk 4.0中尝试激活和停用onResume和onPause中的应用程序,如下所示:

@Override
protected void onResume() {
    super.onResume();

    // update your UI

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

@Override
protected void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}