不能够呼吁其他activitys的Facebook功能 [英] Not able to call facebook feature on other activitys
问题描述
我使用Android SDK中的Facebook API的工作。在这里,现在面临一个问题:
如果我住在同一个活动(单活动),我能够使用像华尔街邮报的功能,
应用程序请求等,但如果我想一些其他活动的那些特征说第三/第四个活动(在导航),我不能够访问该页面的华尔街邮报,应用程序的要求等。
谁能帮我出吗?请告诉我,如果我失去了一些东西。在每一项活动的onCreate()方法,I M使用setconnection()和的getID()。比我用菜单调用Facebook的功能,从我的Facebook活动。
下面是我的code:
公共无效setConnection(){
mContext =这一点;
mFacebook =新的Facebook(getResources()的getString(R.string.FACEBOOK_ID_TEST));
mAsyncRunner =新AsyncFacebookRunner(mFacebook); } 公共布尔isSession(){
共享preFS = preferenceManager.getDefaultShared preferences(mContext);
串的access_token =共享prefs.getString(的access_token,×);
龙期满=共享prefs.getLong(access_expires,-1);
Log.d(TAG,的access_token); 如果(ACCESS_TOKEN = NULL&放大器;!&安培;!到期= -1){
mFacebook.setAccessToken(的access_token);
mFacebook.setAccessExpires(到期);
}
返回mFacebook.isSessionValid();
} 公共无效的getID()
{ 束束=新包();
bundle.putString(域,生日);
尝试{
mFacebook.request(我的/朋友,包);
}赶上(MalformedURLException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
Log.e(在Facebook上的朋友列表错误,异常=+ e.getMessage());
e.printStackTrace();
}
如果(isSession()){
Log.d(TAGsessionValid);
尝试{
mFacebook.request(我的/朋友,包);
}赶上(MalformedURLException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
Log.e(在Facebook上的朋友列表错误,异常=+ e.getMessage());
e.printStackTrace();
}
}其他{
//没有登录,所以重新登录
Log.d(TAG,sessionNOTValid,重新登录);
mFacebook.authorize(这,权限,新LoginDialogListener());
}
}
您不应该为每个活动的新的Facebook对象。我建议有静态Facebook的对象,你在你的主要活动初始化工具类,并使用这个对象,虽然其他的活动。
例如。
类公用程式{
公共静态的Facebook mFacebook; 公共静态无效的initialize(){
mFacebook =新的Facebook(APP_ID);
}
}
您在您的主要活动的onCreate调用Utility.initialize()()
和你需要的地方让Facebook API调用,您可以使用:
Utility.mFacebook.request(....)。
希望这是有帮助的。
I am working with Facebook API using Android SDk. Here am facing one problem :
If I reside in same Activity (Single Activity) I am able to use the features like "Wall Post", "App Request" etc. But If I want those features on some other activity say third/fourth activity (upon navigation) I am not able to get access to the pages for "Wall Post" , "App request" etc. Can anyone help me out of this ? Please tell me if I am missing something. In every activity's oncreate() method, i m using setconnection() and getID(). Than I am calling facebook features from my facebook activity by using Menus.
Below is my code :
public void setConnection() {
mContext = this;
mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
public void getID()
{
Bundle bundle = new Bundle();
bundle.putString("fields", "birthday");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
if (isSession()) {
Log.d(TAG, "sessionValid");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}
}
You should not create new Facebook object for each activity. I recommend have a Utility class with static Facebook object which you initialize in your main activity and use this object though other activities.
E.g.
Class Utiltiy {
public static Facebook mFacebook;
public static void initialize() {
mFacebook = new Facebook(app_id);
}
}
You call Utility.initialize() in your main activity's onCreate()
and wherever you need to make Facebook API call, you can use:
Utility.mFacebook.request(....).
Hope this is helpful.
这篇关于不能够呼吁其他activitys的Facebook功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!