2014-11-09 55 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 
    } 

} 
相关问题