其他分享
首页 > 其他分享> > android – 保存有关Facebook用户的信息,以便在另一个Activity中使用

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

作者:互联网

我正在为我的大学做项目,我想知道如何保存关于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 one rror(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();
}

解决方法:

如果要将信息发送到下一个活动,可以使用捆绑包将其添加到意图中.

活动:

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);

标签:android,facebook,facebook-sdk-4-0
来源: https://codeday.me/bug/20190519/1137645.html