Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果用户一旦登录,如何在android应用程序中自动登录Facebook?_Android_Facebook_Facebook Graph Api - Fatal编程技术网

如果用户一旦登录,如何在android应用程序中自动登录Facebook?

如果用户一旦登录,如何在android应用程序中自动登录Facebook?,android,facebook,facebook-graph-api,Android,Facebook,Facebook Graph Api,我在android应用程序中使用graph api连接到facebook。现在我要做的是为我的facebook类创建一个实例,并每次从main activity调用login方法。如果用户使用该应用程序登录一次,下次打开应用程序时,它应该使用上一个活跃的facebook会话直接登录..怎么做?Sombody请帮助 登录按钮点击方式 public void onBtnClicked(View v){ if(v.getId() == R.id.btnLogin)

我在android应用程序中使用graph api连接到facebook。现在我要做的是为我的facebook类创建一个实例,并每次从main activity调用login方法。如果用户使用该应用程序登录一次,下次打开应用程序时,它应该使用上一个活跃的facebook会话直接登录..怎么做?Sombody请帮助

登录按钮点击方式

public void onBtnClicked(View v){
            if(v.getId() == R.id.btnLogin)
            {

                FacebookClass na = new FacebookClass(this);
                na.login();
             }
}
这是我的登录方式

public void login() 
{
    // start Facebook Login
    Session.openActiveSession(mContext, true, new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
             if (session.isOpened()) {


                        sessionState = true;


                 Toast.makeText(mContext, "Access token :" + session.getAccessToken() + "\n" +"Expires at "+session.getExpirationDate().toLocaleString(), Toast.LENGTH_LONG).show();

                      Log.i("sessionToken", session.getAccessToken());
                      Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());

                     access_token =session.getAccessToken();

                     pref = mContext.getPreferences(0);
                     edt = pref.edit();

                      Editor prefsEditor = pref.edit();
                      edt.putString("Access token", access_token);
                      edt.commit();

                /**
                 * getting user's name and location
                 */

                 // make request to the /me API
                  Request.newMeRequest(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                         if (user != null)
                         {
                              dispname = user.getName();
                              id = user.getId();

                              Object userLocation = user.getLocation();

                              Log.i("Profile information", ""+response);

                             String strLocation = (userLocation != null)? ((GraphObject) userLocation).getProperty("name").toString() : "No location found";


                                //location.setText("Lives in " + strLocation + "!");

                               disploc = strLocation;

                               edt.putString("Username", dispname);
                               edt.putString("Location", disploc);
                               edt.commit();

                           ((MainActivity)mContext).dispatchInformations(dispname,disploc);


                              }
                    }
                  }).executeAsync();



                  /**
                   * Getting user's profile picture
                   */
                  Bundle params = new Bundle();
                  params.putBoolean("redirect", false);
                  params.putString("type", "normal");
                  params.putString("height", "200");
                  params.putString("width", "200");
                  new Request(
                          Session.getActiveSession(),
                            "/me/picture",
                            params,
                            HttpMethod.GET,
                            new Request.Callback() {
                                public void onCompleted(Response response) {
                                    /* handle the result */
                                    Log.i("Profile pic", ""+response);
                                    GraphObject graph=response.getGraphObject();
                                    JSONObject jsonObj=graph.getInnerJSONObject();
                                    JSONObject object;
                                    String imageURL;



                                    try {
                                         object = jsonObj.getJSONObject("data");
                                          imageURL=object.getString("url");

                                          new DownloadImageTask().execute(imageURL);


                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

                                }
                            }
                        ).executeAsync();

             }





}

    private boolean isSubsetOf(Collection<String> subset,
            Collection<String> superset) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }
        }
        return true;

 }

 });

}
public void login()
{
//启动Facebook登录
openActiveSession(mContext,true,new Session.StatusCallback()){
@凌驾
公共无效调用(会话、会话状态、,
例外情况(例外情况){
if(session.isOpened()){
sessionState=true;
Toast.makeText(mContext,“访问令牌:+session.getAccessToken()+”\n“+”在“+session.getExpirationDate().toLocaleString(),Toast.LENGTH_LONG.show()过期);
Log.i(“sessionToken”,session.getAccessToken());
Log.i(“sessionTokenDueDate”,session.getExpirationDate().toLocalString());
access_token=session.getAccessToken();
pref=mContext.getPreferences(0);
edt=预编辑();
编辑器prefsEditor=pref.edit();
edt.putString(“访问令牌”,访问令牌);
提交();
/**
*获取用户名和位置
*/
//向/me API发出请求
Request.newMeRequest(会话,new Request.GraphUserCallback(){
//带有用户对象的Graph API响应后的回调
@凌驾
未完成公共无效(GraphUser用户,响应){
如果(用户!=null)
{
dispname=user.getName();
id=user.getId();
Object userLocation=user.getLocation();
Log.i(“配置文件信息”,“响应”);
字符串strLocation=(userLocation!=null)?((GraphObject)userLocation.getProperty(“name”).toString():“未找到位置”;
//location.setText(“居住在”+strLocation+“!”);
displace=strLocation;
edt.putString(“用户名”,dispname);
edt.putString(“位置”,显示);
提交();
((主活动)mContext)。调度信息(调度名称、显示);
}
}
}).executeAsync();
/**
*获取用户的个人资料图片
*/
Bundle params=新Bundle();
参数putBoolean(“重定向”,false);
参数putString(“类型”、“正常”);
参数putString(“高度”、“200”);
参数putString(“宽度”,“200”);
新要求(
Session.getActiveSession(),
“/me/picture”,
params,
HttpMethod.GET,
新的Request.Callback(){
未完成公共无效(响应){
/*处理结果*/
Log.i(“配置文件pic”,“”+响应);
GraphObject graph=response.getGraphObject();
JSONObject jsonObj=graph.getInnerJSONObject();
JSONObject对象;
字符串imageURL;
试一试{
object=jsonObj.getJSONObject(“数据”);
imageURL=object.getString(“url”);
新建DownloadImageTask().execute(imageURL);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
).executeAsync();
}
}
私有布尔isSubsetOf(集合子集,
收集超集){
for(字符串:子集){
如果(!superset.contains(string)){
返回false;
}
}
返回true;
}
});
}

您可以使用
Session.getActiveSession()进行检查

例如
onCompleted()中使用此方法

Session facebooksession = Session.getActiveSession();
if(facebooksession != null) {
//Second time login
}
else{
//First time login
}

第一次登录时,在sharedpref中设置。然后再次检查sharedpref值是否可用。如果是,则使用该值登录facebook。我总是得到空值:(但它已登录…如何修复此问题?是否有与onActivityResult相关的内容?请help@Nevaeh您在
onCompleted()中添加了我的代码
method?如果用户第一次通过你的应用程序登录,那么如果同一用户第二次登录应用程序,那么它将为null。我会解释。我有一个用于facebook登录和获取详细信息的类。我已经在我的主活动中编写了onActivityResult方法。问题中的第一个代码片段来自我的主活动,第二个代码片段来自我的主活动是来自我的facebook类。所以,我的问题是,我想检查应用启动时是否有任何活动会话。如果是,则我想在左侧菜单中自动显示名称和图片。否则,