2015-05-04 31 views
5

我正在爲我的大學工作,我想知道如何保存一些關於Facebook用戶的信息,如姓名,電子郵件和圖片。那我怎麼能在另一個活動中使用這些信息呢?正如你可以看到我的代碼,我可以在我登錄同一活動的信息,但我不能有其他活動保存有關在另一個活動中使用的Facebook用戶的信息

MainActivity.java

public class MainActivity extends ActionBarActivity { 

public static final int INDEX_SIMPLE_LOGIN = 0; 
public static final int INDEX_CUSTOM_LOGIN = 1; 

private static final String STATE_SELECTED_FRAGMENT_INDEX = "selected_fragment_index"; 
public static final String FRAGMENT_TAG = "fragment_tag"; 
private FragmentManager mFragmentManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mFragmentManager = getSupportFragmentManager(); 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    if (id == R.id.action_simple_login) { 
     toggleFragment(INDEX_SIMPLE_LOGIN); 
     return true; 
    } 
    if (id == R.id.action_custom_login) { 
     toggleFragment(INDEX_CUSTOM_LOGIN); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

private void toggleFragment(int index) { 
    Fragment fragment = mFragmentManager.findFragmentByTag(FRAGMENT_TAG); 
    FragmentTransaction transaction = mFragmentManager.beginTransaction(); 
    switch (index) { 
     case INDEX_SIMPLE_LOGIN: 
      transaction.replace(android.R.id.content, new FragmentSimpleLoginButton(), FRAGMENT_TAG); 
      break; 
     case INDEX_CUSTOM_LOGIN: 
      transaction.replace(android.R.id.content, new FragmentCustomLoginButton(), FRAGMENT_TAG); 
      break; 
    } 
    transaction.commit(); 
} 

}

MyApplication.java

public class MyApplication extends Application { 
@Override 
public void onCreate() { 
    super.onCreate(); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
} 

/** 
* Call this method inside onCreate once to get your hash key 
*/ 
public void printKeyHash() { 
    try { 
     PackageInfo info = getPackageManager().getPackageInfo("vivz.slidenerd.facebookv40helloworld", PackageManager.GET_SIGNATURES); 
     for (Signature signature : info.signatures) { 
      MessageDigest md = MessageDigest.getInstance("SHA"); 
      md.update(signature.toByteArray()); 
      Log.e("VIVZ", Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
     } 
    } catch (PackageManager.NameNotFoundException e) { 

    } catch (NoSuchAlgorithmException e) { 

    } 
} 

}

FragmentSimpleLoginButton.java

public class FragmentSimpleLoginButton extends Fragment { 
private TextView mTextDetails; 
private CallbackManager mCallbackManager; 
private AccessTokenTracker mTokenTracker; 
private ProfileTracker mProfileTracker; 
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() { 
    @Override 
    public void onSuccess(LoginResult loginResult) { 
     Log.d("VIVZ", "onSuccess"); 
     AccessToken accessToken = loginResult.getAccessToken(); 
     Profile profile = Profile.getCurrentProfile(); 
     mTextDetails.setText(constructWelcomeMessage(profile)); 
    } 

    @Override 
    public void onCancel() { 
     Log.d("VIVZ", "onCancel"); 
    } 

    @Override 
    public void onError(FacebookException e) { 
     Log.d("VIVZ", "onError " + e); 
    } 
}; 


public FragmentSimpleLoginButton() { 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mCallbackManager = CallbackManager.Factory.create(); 
    setupTokenTracker(); 
    setupProfileTracker(); 

    mTokenTracker.startTracking(); 
    mProfileTracker.startTracking(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_simple_login_button, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    setupTextDetails(view); 
    setupLoginButton(view); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Profile profile = Profile.getCurrentProfile(); 
    mTextDetails.setText(constructWelcomeMessage(profile)); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    mTokenTracker.stopTracking(); 
    mProfileTracker.stopTracking(); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    mCallbackManager.onActivityResult(requestCode, resultCode, data); 
} 

private void setupTextDetails(View view) { 
    mTextDetails = (TextView) view.findViewById(R.id.text_details); 
} 

private void setupTokenTracker() { 
    mTokenTracker = new AccessTokenTracker() { 
     @Override 
     protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) { 
      Log.d("VIVZ", "" + currentAccessToken); 
     } 
    }; 
} 

private void setupProfileTracker() { 
    mProfileTracker = new ProfileTracker() { 
     @Override 
     protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
      Log.d("VIVZ", "" + currentProfile); 
      mTextDetails.setText(constructWelcomeMessage(currentProfile)); 
     } 
    }; 
} 

private void setupLoginButton(View view) { 
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button); 
    mButtonLogin.setFragment(this); 
    mButtonLogin.setReadPermissions("user_friends"); 
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback); 
} 

private String constructWelcomeMessage(Profile profile) { 
    StringBuffer stringBuffer = new StringBuffer(); 
    if (profile != null) { 
     stringBuffer.append("Welcome " + profile.getName()); 
    } 
    return stringBuffer.toString(); 
} 
+1

店與ImageLink,電子郵件和共享圖片的偏好,而在另一個活動 – kgandroid

+0

檢索它們。如果你想永久保存它,然後用'SharedPreference',或者如果您收到此郵件,值ImageLink的每個會話那麼最好只是將這些數據傳遞給其他活動使用'意圖' – Kunu

+0

你好,隊友你從FB信息獲得電子郵件地址,以及如何?如果你有電子郵件,你可以分享它的示例代碼 –

回答

4

如果您想將信息發送到下一個活動,您可以將其添加到意圖的捆綁。

活動:

Intent i=new Intent(Activity.this, SecontActivity.class); 
i.putExtra("email", email); 
startActivity(i); 

SecontActivity:

Intent intent = getIntent(); 
String email = intent.getStringExtra("email"); 

如果你想在所有的活動的信息,然後將其保存在SharedPreferences

活動:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putString("email", email); 
editor.commit(); 

SecondActivity:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
String email = sharedPref.getString(email, defaultValue); 
+0

如何從片段獲得上下文? –

+0

call getActivity() – ilan

1

商店的信息,如姓名,IMAGEURL和其他個人資料詳細信息的數據庫sharedpreferencesindicated here)跨不同活動的機會。除非應用程序的數據被清除,否則這些信息可以在應用程序歷史記錄中保存的任何階段獲取。

我會去數據庫,因爲它會更多的結構,並可能提供多用戶支持,因此你想在你的應用程序中實現配置文件切換。

相關問題