好的,我正在尝试来自 Google 的代码 https://developers.google.com/identity/sign-in/android/sign-in?configured=true
登录 Activity
GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
SignInButton signIn_btn;
private static final int RC_SIGN_IN = 0;
ProgressDialog progress_dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buidNewGoogleApiClient();
setContentView(R.layout.activity_login);
customizeSignBtn();
setBtnClickListeners();
mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(this.NOTIFICATION_SERVICE);
Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "Big Daddy St.ttf");
Typeface myTypeFaceFacebook = Typeface.createFromAsset(getAssets(), "Big Daddy St.ttf");
mainImage = (ImageView) findViewById(R.id.imageView1);
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setOnClickListener(this);
textView1.setTypeface(myTypeFace);
Button button = (Button) findViewById(R.id.btn_login_facebook);
button.setOnClickListener(this);
button.setTypeface(myTypeFaceFacebook);
}
private void buidNewGoogleApiClient() {
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
private void customizeSignBtn() {
signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);
signIn_btn.setSize(SignInButton.SIZE_STANDARD);
signIn_btn.setScopes(gso.getScopeArray());
}
private void setBtnClickListeners() {
// Button listeners
signIn_btn.setOnClickListener(this);
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
}
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
getSignInResult(result);
}
}
private void getSignInResult(GoogleSignInResult result) {
GoogleSignInStatusCodes.getStatusCodeString(result.getStatus().getStatusCode());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
Toast.makeText(this, "email: " + acct.getEmail(), Toast.LENGTH_SHORT).show();
//TextView user_name= (TextView)findViewById(R.id.userName);
// TextView email_id= (TextView)findViewById(R.id.emailId);
// user_name.setText("UserName: "+ acct.getDisplayName());
// email_id.setText("Email Id: " + acct.getEmail());
progress_dialog.dismiss();
} else {
// Signed out, show unauthenticated UI.
try {
} catch (Exception e) {
Toast.makeText(this, "Exception: " +e.getMessage(), Toast.LENGTH_LONG).show();
}
//updateUI(false);
}
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.textView1){
Intent signupIntent = new Intent(this, SignUpScreen.class);
startActivity(signupIntent);
}
if(view.getId() == R.id.btn_login_facebook){
Intent intent = new Intent(this, todelete.class);
//Notification
// notificationWithButton();
startActivity(intent);
}
if(view.getId()==R.id.sign_in_button){
Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();
gSignIn();
}
}
private void gSignIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
// progress_dialog.show();
}
void notificationWithButton(){
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// Prepare intent which is triggered if the notification button is pressed
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Building the notifcation
NotificationCompat.Builder nBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle("Button notification") // notification title
.setContentText("Expand to show the buttons...") // content text
.setTicker("Showing button notification") // status bar message
.addAction(R.drawable.common_google_signin_btn_icon_dark, "Accept", pIntent) // accept notification button
.addAction(R.drawable.common_google_signin_btn_icon_dark_normal, "Cancel", pIntent); // cancel notification button
mNotificationManager.notify(1001, nBuilder.build());
} else {
Toast.makeText(this, "You need a higher version", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
list .xml
<uses-permission android:name="android.permission.INTERNET" />
<!-- To access accounts configured on device -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- To use account credentials -->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WIFI" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
/>
build.gradle(项目)
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
build.gradle(应用程序)
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.jakewharton:butterknife:8.1.0'
compile 'com.google.android.gms:play-services-auth:9.6.1'
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.android.support:support-v4:24.2.1'
}
apply plugin: 'com.google.gms.google-services'
非常感谢您的帮助。
编辑:添加了 GoogleSignInStatusCodes.getStatusCodeString(result.getStatus().getStatusCode()); 返回 10,对应于 DEVELOPER_ERROR。
我之前已经为 Google map 创建了一个 key ,它包含在我的项目中(在 list 文件中,元标记中),现在我必须创建另一个 key 吗?如果是,我该如何包含它?
最佳答案
试试下面的代码
private void handleSignInResult(GoogleSignInResult result) {
Log.d(String.valueOf(LoginActivity.this), "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
if (acct != null) {
Intent iWelcomeActivity = new Intent(MainActivity.this, WelcomeActivity.class);
iWelcomeActivity.putExtra("USERNAME", acct.getDisplayName());
iWelcomeActivity.putExtra("EMAIL", acct.getEmail();
);
startActivity(iWelcomeActivity);
}
} else {
// Signed out, show unauthenticated UI.
//updateUI(false);
}
}
当用户点击登录按钮时, Intent 会提示用户选择要登录的 Google 帐户。
onActivityResult 方法,检索登录结果并调用 handleSignInResult(GoogleSignInResult result)
。
如果登录成功,您可以调用getSignInAccount 方法
来获取包含登录用户信息的GoogleSignInAccount 对象。
使用 GoogleSignInAccount 对象 以 acct.getDisplayName(),acct.getEmail()
的形式检索信息,并将其传递给下一个 Activity ,如上面的代码所示。
希望对你有帮助
关于android - 在 Android 应用程序中集成 Google 登录按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40448078/