2014-11-09 43 views
1

我能夠從每個單一活動獲取數據,但一次從兩個活動調用時都無法獲取數據。無法從兩個不同的活動獲取包數據

我有3個活動

1.GoogleLogin 2.FacebookLogin 3.MainActivity

我有這個在我的GoogleLogin:

@Override 
    public void onConnected(Bundle connectionHint) { 
    // Reaching onConnected means we consider the user signed in. 

    Intent i = new Intent(getApplicationContext(),MainActivity.class); 
    Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 
    //Create the bundle 
    Bundle bundle = new Bundle(); 
    //Add your data from getFactualResults method to bundle 
    bundle.putString("Google", "Logged in using Google Account"); 
    bundle.putString("GoogleUsername", currentUser.getDisplayName()); 
    //Add the bundle to the intent 
    i.putExtras(bundle); 
    startActivity(i); 


    // Indicate that the sign in process is complete. 
    mSignInProgress = STATE_DEFAULT; 
    } 

我有這個在我的FacebookLogin:

  if (session.isOpened()) { 

      // make request to the /me API 
      Request.newMeRequest(session, new Request.GraphUserCallback() { 

      // callback after Graph API response with user object 
      @Override 
      public void onCompleted(GraphUser user, Response response) { 
       if (user != null) { 

       Intent i = new Intent(FBMainActivity.this,MainActivity.class); 
        //Create the bundle 
        Bundle bundle = new Bundle(); 
        //Add your data from getFactualResults method to bundle 
        bundle.putString("Facebook", "Logged in using Facebook Account"); 
        bundle.putString("LastName", user.getLastName()); 
        bundle.putString("FirstName", user.getFirstName()); 
        //Add the bundle to the intent 
        i.putExtras(bundle); 
        startActivity(i); 
       } 
      } 
      }).executeAsync(); 
     } 

In My MainActivit y我得到他們如圖所示:

 // 1. get passed intent 
     Intent facebookintent = getIntent(); 

     // 2. get message value from intent 
     String lastName = facebookintent.getStringExtra("LastName"); 
     String firstName = facebookintent.getStringExtra("FirstName"); 

     // 3. get bundle from intent 
     Bundle facebookbundle = facebookintent.getExtras(); 

     // 4. get status value from bundle 
     String facebookstatus = facebookbundle.getString("Facebook"); 



     // 1. get passed intent 
     Intent googleintent = getIntent(); 

     // 2. get message value from intent 
     String userName = googleintent.getStringExtra("GoogleUsername"); 

     // 3. get bundle from intent 
     Bundle googlebundle = facebookintent.getExtras(); 

     // 4. get status value from bundle 
     String googlestatus = googlebundle.getString("Google"); 

     if (facebookstatus=="Logged in using Facebook Account"){ 

      // 3. show message on textView 
      ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName); 

     }else if (googlestatus=="Logged in using Google Account"){ 

      // 3. show message on textView 
      ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName); 
     } 

我無法獲取GoogleUsername,但能夠從每個單一活動能夠調用時單獨獲取它們。

+0

你想做什麼?你如何從同時間的另外兩項活動開始一項活動? – 2014-11-09 06:38:41

+0

其實我想檢查用戶登錄哪個賬戶並從他的賬戶獲得他的用戶名 – coder 2014-11-09 06:39:39

+0

你需要檢查你的包含鑰匙,'Facebook'或'Google'的包,然後從那裏獲取數據,當你登錄與其中一人,你需要檢查 – 2014-11-09 06:44:14

回答

2

根據邏輯流程,您的活動可以通過facebookgoogle登錄開始。 所以你必須檢查和適當使用它。做這樣的事情

// 1. get passed intent 
    Intent intent = getIntent(); 

    if (intent.getStringExtra("Facebook") != null){ 

    // 2. get message value from intent 
    String lastName = intent.getStringExtra("LastName"); 
    String firstName = intent.getStringExtra("FirstName"); 

    if(intent.getStringExtra("Facebook").equals("Logged in using Facebook Account")){ 
     ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName); 
    } 
}else if(intent.getStringExtra("Google") != null){ 


    // 2. get message value from intent 
    String userName = googleintent.getStringExtra("GoogleUsername"); 

    // 3. get bundle from intent 
    Bundle googlebundle = facebookintent.getExtras(); 

    if(intent.getStringExtra("Google").equals("Logged in using Google Account")){ 
     ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName); 
    } 

} 
1

您需要先檢查googleStatus和facebookStatus。 (在Java中,我們不比較兩個字符串==,比較,與.equals()法)

你需要檢查組合中的谷歌帳戶存在或Facebook帳戶,爲此你需要下面的代碼:

if (bundle != null) 
{ 

    if (bundle.containsKey("Facebook")) 
    {  
     // user logged in with facebook account 
    } 
    else if (bundle.containsKey("Google")) 
    { 
     // check google account 
    } 

} 
相關問題