问题
I have a native Android app that needs to connect to a different Facebook app (different Application ID) based on an app setting that can be changed at runtime.
Imagine the app can be set to point to DEV, TEST, or PROD. When pointed to DEV the FB Application ID should be "1". When pointed to TEST the FB Application ID should be "2". etc.
The problem is the UiLifecycleHelper from the Facebook SDK automatically reads the "com.facebook.sdk.ApplicationId" from the AndroidManifest.xml during the onCreate phase. There doesn't appear to be a way to set this programmatically.
Is it possible to use UiLifecycleHelper and point to a different application ID at runtime or do I have to fall back to manually managing the Session?
回答1:
Hello all i know above answer is right for older sdk version
, but in facebook sdk 4
there is no Session
class
You can simply do it by using this single line code:
FacebookSdk.setApplicationId(APP_ID);
Thanks!
回答2:
Here what i used to set the application id programatcally
private Session.StatusCallback statusCallback = new SessionStatusCallback();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s=""+R.string.app_id;
Session session = new Session.Builder(getBaseContext()).setApplicationId(s).build();
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
if(session.isOpened()){
//Do your task
}
}
}
回答3:
I decided to fork the facebook-android-sdk project on GitHub and make the necessary changes. I have submitted a pull request with my changes if anyone is interested, https://github.com/facebook/facebook-android-sdk/pull/294.
回答4:
Yes, you can set it programmatically. Use the Sessions$Builder class which allows you to build a session without needing to use one of the private constructors.
Session session = new Session.Builder(context).setApplicationId(applicationId).build();
回答5:
I think there is a simpler way to do this, just call the static method setApplicationId com.facebook.Settings.setApplicationId(facebookID);
And you're good to go. No need to create a session manually with the Builder and set it as the active session!
Details of the Flaw:
The facebookID you set in the settings class will be used by getMetadataApplicationId
method of com.facebook.internal.Utility
public static String getMetadataApplicationId(Context context) {
Validate.notNull(context, "context");
Settings.loadDefaultsFromMetadata(context);
return Settings.getApplicationId();
}
Which in turn will be used by all the calls to create a Session:
Session(Context context, String applicationId, TokenCachingStrategy tokenCachingStrategy,
boolean loadTokenFromCache) {
// if the application ID passed in is null, try to get it from the
// meta-data in the manifest.
if ((context != null) && (applicationId == null)) {
applicationId = Utility.getMetadataApplicationId(context);
}
Validate.notNull(applicationId, "applicationId");
.
.
.
}
Cheers.
来源:https://stackoverflow.com/questions/15072937/facebook-sdk-for-android-set-application-id-programmatically