如何重定向到下一个活动

时间:2014-10-15 17:18:06

标签: android facebook facebook-graph-api android-activity facebook-login

我正在创建一个Android应用程序,用户应该通过FACEBOOK登录。问题是,当用户通过FACEBOOK登录时,他会从他登录的位置返回到相同的Activity,而不是转到下一个Activity。这是我的代码: -

MainActivity

package com.example.abc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class MainActivity extends Activity {

    // Your Facebook APP ID
    private static String APP_ID = "xxxxxxxxxxxxxxxx"; // Replace with your App ID

    private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);

    /** Getting a reference to the ViewPager defined the layout file */        
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);
    Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                //Intent in = new Intent(MainActivity.this, LoginFaceBook.class);
                //startActivity(in);
                loginToFacebook();

        }
    });

    Button btn2 = (Button) findViewById(R.id.button2);
    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            loginToTwitter();
        }

        private void loginToTwitter() {
            // TODO Auto-generated method stub

        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);




        Intent inst = new Intent(getApplicationContext(), AnotherActivity.class);
        startActivity(inst);


        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();

                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
   }
}

1 个答案:

答案 0 :(得分:1)

您似乎获得了null访问令牌,然后将您发送给FB进行授权,跳过启动下一个活动的位置。在onComplete的{​​{1}}方法中,您提交了访问令牌和DialogListener并退出SharedPreferences方法。

进入下一个活动的一种策略可能是在您调用它之后尝试在loginToFacebook方法的 之外启动它(检查以确保您确实获得了访问权限)令牌回来,当然)。