2
由於以前使用facebook的片段登錄和現在的需求變化很好,所以現在facebook登錄活動,所以我們有正常的android按鈕,並點擊登錄對話框將出現用於用戶交互的用戶名和密碼,以及當登錄成功時請求用executeme獲取用戶詳細信息。每個代碼都完成,但是當點擊按鈕對話框時不會出現,但onsession狀態將日誌顯示爲輸出爲facebook sdk 3.0登錄android活動類
第一顯示打開 比顯示登錄失敗關閉
所有的步驟都需要登錄facebook在facebook上提到應用程序開發站點執行,所有需要的類和元數據信息也在andriod清單文件中給出。
這裏的活性類進行登錄
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的」, 「」 +狀態);這裏顯示LOGIN FAILED CLOSED。
和清單文件
<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>
所以在這裏我]丟個板磚.Thaks任何答覆