Created
June 24, 2016 21:18
-
-
Save alvareztech/4393f863e2b9890b5c35c134632c4bef to your computer and use it in GitHub Desktop.
Login Facebook en Android, código usado en el video: https://www.youtube.com/watch?v=1HgM_vc-rSc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.today.LoginActivity"> | |
<com.facebook.login.widget.LoginButton | |
android:id="@+id/loginButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:paddingBottom="10dp" | |
android:paddingTop="10dp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.today.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="@string/welcome" | |
android:textSize="20sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:onClick="logout" | |
android:text="@string/logout" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="tech.alvarez.today"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:name=".TodayApp" | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<meta-data | |
android:name="com.facebook.sdk.ApplicationId" | |
android:value="@string/facebook_app_id" /> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".LoginActivity"></activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 24 | |
buildToolsVersion "24.0.0" | |
defaultConfig { | |
applicationId "tech.alvarez.today" | |
minSdkVersion 15 | |
targetSdkVersion 24 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:24.0.0' | |
compile 'com.facebook.android:facebook-android-sdk:[4,5)' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.today; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.Toast; | |
import com.facebook.CallbackManager; | |
import com.facebook.FacebookCallback; | |
import com.facebook.FacebookException; | |
import com.facebook.login.LoginResult; | |
import com.facebook.login.widget.LoginButton; | |
import java.util.Arrays; | |
public class LoginActivity extends AppCompatActivity { | |
private LoginButton loginButton; | |
private CallbackManager callbackManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
callbackManager = CallbackManager.Factory.create(); | |
loginButton = (LoginButton) findViewById(R.id.loginButton); | |
loginButton.setReadPermissions(Arrays.asList("email")); | |
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { | |
@Override | |
public void onSuccess(LoginResult loginResult) { | |
goMainScreen(); | |
} | |
@Override | |
public void onCancel() { | |
Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onError(FacebookException error) { | |
Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
private void goMainScreen() { | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
callbackManager.onActivityResult(requestCode, resultCode, data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.today; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import com.facebook.AccessToken; | |
import com.facebook.login.LoginManager; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (AccessToken.getCurrentAccessToken() == null) { | |
goLoginScreen(); | |
} | |
} | |
private void goLoginScreen() { | |
Intent intent = new Intent(this, LoginActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
public void logout(View view) { | |
LoginManager.getInstance().logOut(); | |
goLoginScreen(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">Today</string> | |
<string name="facebook_app_id">aqui su id app</string> | |
<string name="welcome">Bienvenido</string> | |
<string name="cancel_login">Se canceló la operación</string> | |
<string name="error_login">Ocurrió un error al ingresar</string> | |
<string name="logout">Cerrar sesión</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.today; | |
import android.app.Application; | |
import com.facebook.FacebookSdk; | |
import com.facebook.appevents.AppEventsLogger; | |
/** | |
* Created by Daniel Alvarez on 21/6/16. | |
* Copyright © 2016 Alvarez.tech. All rights reserved. | |
*/ | |
public class TodayApp extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
FacebookSdk.sdkInitialize(getApplicationContext()); | |
AppEventsLogger.activateApp(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excelente! Muchas gracias! vi tu video en tu canal y me ayudo mucho! El de login y el de configurar el SDK