Estou criando uma aplicação android que faz login com facebook. Isto já está funcionando, mas está acontecendo algo que não estou conseguindo resolver.
Segundo os exemplos da própria documentação do facebook, o código que faz as chamadas pra api do facebook fica dentro do método onCreate()
, e isso faz com que assim que eu abra a página de login (que tem o botão de login com facebook) ele já abre automaticamente o facebook pra pedir as permissões mesmo sem eu ter clicado no botão de fazer login com facebook.
O que eu faço para que ele só abra a tela de login quando eu clicar no botão de "Log in with Facebook"?
Seguem os códigos:
LoginActivity.java
public class LoginActivity extends Activity {
CallbackManager callbackManager;
LoginButton loginButton;
LoginManager loginManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("Teste", "onCreate inicio");
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("Teste", "onSuccess inicio [Token = " + loginResult.getAccessToken() + "], [Permissões = " + loginResult.getRecentlyGrantedPermissions() +
"], [Negações = " + loginResult.getRecentlyDeniedPermissions() + "] Json: "+ loginResult.toString());
}
@Override
public void onCancel() {
// App code
Log.d("Teste", "onCancel inicio");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d("Teste", "onError( " + exception.toString() + ")");
}
});
}
activity_login.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F76B03"
android:scrollbars="none"
android:gravity="center">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"
android:text="ou"
android:textColor="#000000" />
<EditText style="@style/tvFullParent"
android:id="@+id/etLogin"
android:background="@drawable/shapeedittext"
android:inputType="textEmailAddress"
android:drawableLeft="@drawable/ic_action_user"
android:hint="E-mail"/>
<EditText style="@style/tvFullParent"
android:id="@+id/etPassword"
android:background="@drawable/shapeedittext"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_action_lock_closed"
android:hint="Senha"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:text="@string/btnLogin"/>
androidmanifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:screenOrientation="portrait">
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
</application>