我正在为我的大学工作,我想知道如何保存关于Facebook用户的一些信息,如姓名,电子邮件和图片。那么我如何在另一个Activity中使用这些信息呢?正如你可以看到我的代码,我可以在我记录的同一活动中显示信息,但我不能在另一个活动中
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();
}
答案 0 :(得分: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);
答案 1 :(得分:1)
将名称,图片和其他个人资料详细信息等信息存储在数据库或( sharedpreferences 为indicated here)中,以便访问不同的活动。除非应用程序中的数据被清除,否则可以在应用程序历史记录中保存的任何阶段获取此信息。
我会选择数据库,因为它可以提供更多结构,并且可以提供多用户支持,因为您希望在应用中实现配置文件切换。