前提・発生している問題・エラーメッセージ
ログイン済みのGoogleアカウントからアカウント名(~@gmail.com)を取得したいです。
ですがnullが返され、アカウント名が取得できません。
該当のソースコード
サンプルコードを元にしていますが、startActivityResult,ActivityResultが非推奨となる可能性があることを見込んで、ActivityResultLauncherで記述しています。
Java
1//サインイン済みのアカウント選択 2 Intent getgsc = credential.newChooseAccountIntent(); 3 4//アカウントが選択されたらアカウント名を取得、キャンセルされたらアプリを終了 5 ActivityResultLauncher<Intent> chooseaccount = registerForActivityResult( 6 new ActivityResultContracts.StartActivityForResult(), 7 new ActivityResultCallback<ActivityResult>(){ 8 @Override 9 public void onActivityResult(ActivityResult result) { 10 if (result.getResultCode() == Activity.RESULT_OK) { 11 String accountName = getgsc.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 12 Toast tst_accountName = Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG); 13 tst_accountName.show(); 14 if (accountName != null) { 15 SharedPreferences settings = 16 getPreferences(Context.MODE_PRIVATE); 17 SharedPreferences.Editor editor = settings.edit(); 18 editor.putString(PREF_ACCOUNT_NAME, accountName); 19 editor.apply(); 20 credential.setSelectedAccountName(accountName); 21 } 22 } else if (result.getResultCode() == Activity.RESULT_CANCELED) { 23 finishAndRemoveTask(); 24 } else { 25 } 26 } 27 }); 28 29 … 30 31 chooseaccount.launch(getgsc); 32
###詳細コード
MainActivity
1package com.example.test; 2 3import android.Manifest; 4import androidx.appcompat.app.AppCompatActivity; 5 6import android.app.Activity; 7import android.os.Bundle; 8import android.app.Dialog; 9import android.content.Context; 10import com.google.android.gms.auth.api.signin.GoogleSignInClient; 11import com.google.android.gms.auth.api.signin.GoogleSignInOptions; 12import com.google.android.gms.auth.api.signin.GoogleSignIn; 13import com.google.android.gms.common.api.Scope; 14import android.content.Intent; 15import androidx.activity.result.ActivityResultCaller; 16import androidx.activity.result.ActivityResultLauncher; 17import androidx.activity.result.ActivityResultCallback; 18import androidx.activity.result.contract.ActivityResultContracts; 19import androidx.activity.result.ActivityResult; 20import androidx.biometric.BiometricManager.Authenticators; 21import android.provider.Settings; 22 23import com.google.api.client.util.ExponentialBackOff; 24import com.google.api.services.sheets.v4.Sheets; 25import com.google.api.services.sheets.v4.model.*; 26import com.google.api.services.sheets.v4.SheetsScopes; 27import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; 28import android.accounts.AccountManager; 29import android.content.SharedPreferences; 30 31import com.google.android.gms.common.AccountPicker; 32import com.google.android.gms.common.AccountPicker.AccountChooserOptions; 33 34//Debug 35import android.widget.Button; 36import android.view.View; 37import android.widget.Toast; 38import android.util.Log; 39import java.util.List; 40import java.util.Arrays; 41import java.io.IOException; 42import java.util.ArrayList; 43import pub.devrel.easypermissions.AfterPermissionGranted; 44import pub.devrel.easypermissions.EasyPermissions; 45 46public class MainActivity extends AppCompatActivity 47 implements Authenticators{ 48 public final int REQUEST_DRIVE = 1; 49 public final int REQUEST_SHEETS = 10; 50 public final int REQUEST_CODE= 100; 51 public final Scope SCOPE_DRIVE = new Scope("https://www.googleapis.com/auth/drive"); 52 public final Scope SCOPE_SHEETS = new Scope("https://www.googleapis.com/auth/spreadsheets"); 53 public final String[] SCOPES_ARRAYS = {SheetsScopes.DRIVE, SheetsScopes.SPREADSHEETS}; 54 public final List<String> SCOPES = Arrays.asList(SCOPES_ARRAYS); 55 public static final String PREF_ACCOUNT_NAME = "accountName"; 56 public static final String accountName = "null"; 57 public GoogleAccountCredential credential; 58 59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 setContentView(R.layout.activity_main); 62 63 credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), SCOPES) 64 .setBackOff(new ExponentialBackOff()); 65 66 Intent getgsc = credential.newChooseAccountIntent(); 67 68 ActivityResultLauncher<Intent> chooseaccount = registerForActivityResult( 69 new ActivityResultContracts.StartActivityForResult(), 70 new ActivityResultCallback<ActivityResult>(){ 71 @Override 72 public void onActivityResult(ActivityResult result) { 73 if (result.getResultCode() == Activity.RESULT_OK && getgsc.getExtras() != null) { 74 String accountName = getgsc.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 75 Toast tst_accountName = Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG); 76 tst_accountName.show(); 77 if (accountName != null) { 78 SharedPreferences settings = 79 getPreferences(Context.MODE_PRIVATE); 80 SharedPreferences.Editor editor = settings.edit(); 81 editor.putString(PREF_ACCOUNT_NAME, accountName); 82 editor.apply(); 83 credential.setSelectedAccountName(accountName); 84 } 85 } else if (result.getResultCode() == Activity.RESULT_CANCELED) { 86 finishAndRemoveTask(); 87 } else { 88 } 89 } 90 }); 91 92 Button btn = findViewById(R.id.switch1); 93 btn.setOnClickListener(new View.OnClickListener(){ 94 public void onClick(View view){ 95 chooseaccount.launch(getgsc); 96 } 97 }); 98 } 99}
layout
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context=".MainActivity"> 10 11 <LinearLayout 12 android:orientation="horizontal" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"> 15 <TextView 16 android:text="スイッチ" 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="3" 20 android:textStyle="bold" 21 android:textSize="20sp"/> 22 <Button 23 android:id="@+id/switch1" 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_weight="1.5" 27 android:text="スイッチ"/> 28 </LinearLayout> 29 30</LinearLayout>
補足情報
Androidバージョン10
Android SDK built for x86_64