简体   繁体   English

用Android Activity类登录Facebook SDK 3.0

[英]facebook sdk 3.0 login with android activity class

As previously used the login for the facebook for the fragment and that work great so with requirement change now facebook login with activity so,we have normal android button and on click of that the login dialog will appear for user interaction for giving username and password and when the login is sucessfull requesting with executeme to get user details.Every code is done but when click on the button dialog not appear but onsessionstate change the log showing output as 由于以前使用该片段的Facebook登录名,并且效果很好,因此随着需求的变化现在可以通过活动登录Facebook,因此,我们有正常的android按钮,单击该登录对话框将出现,用于用户交互,以提供用户名和密码,当登录成功后,用executeme请求获取用户详细信息。完成所有代码,但是当单击按钮对话框时未出现,而是在onsessionstate上,将日志显示为

first show OPENING than show LOGIN FAILED CLOSED 首先显示OPENING比显示LOGIN FAILED CLOSED

all the step require for login in facebook mentioned in facebook app development site is carried out and all the require class and meta data info is also given in andriod manifest file. 在facebook应用程序开发站点中提到的所有在facebook中登录所需的步骤均已执行,并且所有必填类和元数据信息也都在andriod清单文件中给出。

here the activity class for login 这里是用于登录的活动类

public class ResgistrationApp extends Activity {

    EditText username,password,name,nickname,email,phone;

    Button Registertauky,fb_connect;

    com.facebook.Session fbSession;
    ImageView user_image;
    private UiLifecycleHelper uihelper;

    private Session.StatusCallback sessioncallback=new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            // TODO Auto-generated method stub
            Log.e("Sessionstate", ""+state);
            if(session.getState()==SessionState.OPENED)
            {
                makeuserdetailsrequest(session);
            }

        }
    };

    private Request.GraphUserCallback graphcallback=new Request.GraphUserCallback() {

        @Override
        public void onCompleted(GraphUser user, Response response) {
            // TODO Auto-generated method stub
            Log.e("FB RES", ""+response);
            Log.e("UID", ""+user.getId());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 

    {
        super.onCreate(savedInstanceState);
        //Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        setContentView(R.layout.activity_resgistration_app);

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.primus.taukyresigstration", 
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                }
        } catch (NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }

        uihelper=new UiLifecycleHelper(this, sessioncallback);
        uihelper.onCreate(savedInstanceState);

        username=(EditText) findViewById(R.id.register_usernameone);

        password=(EditText) findViewById(R.id.register_password);

        name=(EditText) findViewById(R.id.register_username);

        nickname=(EditText) findViewById(R.id.register_nickname);

        email=(EditText) findViewById(R.id.register_email);

        phone=(EditText) findViewById(R.id.register_phone);

        Registertauky=(Button) findViewById(R.id.Registertauky);

        user_image=(ImageView) findViewById(R.id.register_user_picture);

        fb_connect=(Button) findViewById(R.id.facbook_getinfo);

        fbSession=new Session(this);
        fbSession.addCallback(sessioncallback);


        //This going to register the tauky server

        Registertauky.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });


        //This button click going to connect to the facebook
        fb_connect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub      
            if(fbSession.isOpened())
            {
                fbSession=Session.getActiveSession();
                //do the fetching of user details..
            }
            else
            {
                fbSession=Session.openActiveSession(ResgistrationApp.this, true, sessioncallback);
            }   
            }
        });
    }

    //--------------------------------------------------------------------------------------------------------------//



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        uihelper.onActivityResult(requestCode, resultCode, data);
        //Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }



    class LoginretrieveTask extends AsyncTask<Void, Void, Void>
    {
        Session session;

        public LoginretrieveTask(Session fbsession) {
            // TODO Auto-generated constructor stub
            session=fbsession;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            return null;
        }

    }

    protected void makeuserdetailsrequest(Session session) {
        // TODO Auto-generated method stub

        Request user_request=Request.newMeRequest(session, graphcallback);
        user_request.executeAsync();
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        uihelper.onResume();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        uihelper.onPause();

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        uihelper.onDestroy();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        uihelper.onSaveInstanceState(savedInstanceState);
    }
}

Log.e("Sessionstate", ""+state); Log.e(“ Sessionstate”,“” + state); here showing LOGIN FAILED CLOSED. 这里显示LOGIN FAILED CLOSED。

and the manifest file 和清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.primus.taukyresigstration"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.primus.taukyresigstration.ResgistrationApp"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.LoginActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name">
        </activity>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/appid"/>
    </application>

</manifest>

so can any point me where im doing wrong .Thaks for any reply 所以任何人都可以指出我做错了什么。

Here you can find the solution to login from the activity. 在这里,您可以找到从活动登录的解决方案。

Facebook sdk 3.0.1 is not working properly Facebook SDK 3.0.1无法正常工作

and this may also be helpful to you... 这也可能对您有帮助...

Facebook Integration in Android Application Facebook在Android应用程序中的集成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM