保存有关 Facebook 用户的信息以在另一个 Activity 中使用

save info about facebook user to use in another Activity

我正在为我的大学做项目,我想知道如何保存有关 facebook 用户的一些信息,如姓名、电子邮件和图片。那么如何在另一个 Activity 中使用此信息?正如您所看到的我的代码,我可以在我登录的 activity 中显示信息,但我不能在另一个 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();
}

数据库 或(sharedpreferences as indicated here)中存储名称、图片网址和其他个人资料详细信息用于跨不同活动的访问。一旦保存在整个应用程序的历史记录中,就可以在任何阶段获取此信息,除非清除应用程序中的数据。

我会选择数据库,因为它会更加结构化并且可能提供多用户支持,以防您想在您的应用程序中实现配置文件切换。

如果您想将信息发送到下一个 activity,您可以使用捆绑包将其添加到意图中。

ACTIVITY:

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

第二Activity:

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

如果您想要所有活动中的信息,请将其保存在 SharedPreferences

Activity:

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

第二Activity:

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