teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

詳細コードの明示

2021/12/15 10:02

投稿

bpdjb3
bpdjb3

スコア1

title CHANGED
File without changes
body CHANGED
@@ -39,8 +39,143 @@
39
39
  chooseaccount.launch(getgsc);
40
40
 
41
41
  ```
42
+ ###詳細コード
42
43
 
44
+ ```MainActivity
45
+ package com.example.test;
43
46
 
47
+ import android.Manifest;
48
+ import androidx.appcompat.app.AppCompatActivity;
49
+
50
+ import android.app.Activity;
51
+ import android.os.Bundle;
52
+ import android.app.Dialog;
53
+ import android.content.Context;
54
+ import com.google.android.gms.auth.api.signin.GoogleSignInClient;
55
+ import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
56
+ import com.google.android.gms.auth.api.signin.GoogleSignIn;
57
+ import com.google.android.gms.common.api.Scope;
58
+ import android.content.Intent;
59
+ import androidx.activity.result.ActivityResultCaller;
60
+ import androidx.activity.result.ActivityResultLauncher;
61
+ import androidx.activity.result.ActivityResultCallback;
62
+ import androidx.activity.result.contract.ActivityResultContracts;
63
+ import androidx.activity.result.ActivityResult;
64
+ import androidx.biometric.BiometricManager.Authenticators;
65
+ import android.provider.Settings;
66
+
67
+ import com.google.api.client.util.ExponentialBackOff;
68
+ import com.google.api.services.sheets.v4.Sheets;
69
+ import com.google.api.services.sheets.v4.model.*;
70
+ import com.google.api.services.sheets.v4.SheetsScopes;
71
+ import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
72
+ import android.accounts.AccountManager;
73
+ import android.content.SharedPreferences;
74
+
75
+ import com.google.android.gms.common.AccountPicker;
76
+ import com.google.android.gms.common.AccountPicker.AccountChooserOptions;
77
+
78
+ //Debug
79
+ import android.widget.Button;
80
+ import android.view.View;
81
+ import android.widget.Toast;
82
+ import android.util.Log;
83
+ import java.util.List;
84
+ import java.util.Arrays;
85
+ import java.io.IOException;
86
+ import java.util.ArrayList;
87
+ import pub.devrel.easypermissions.AfterPermissionGranted;
88
+ import pub.devrel.easypermissions.EasyPermissions;
89
+
90
+ public class MainActivity extends AppCompatActivity
91
+ implements Authenticators{
92
+ public final int REQUEST_DRIVE = 1;
93
+ public final int REQUEST_SHEETS = 10;
94
+ public final int REQUEST_CODE= 100;
95
+ public final Scope SCOPE_DRIVE = new Scope("https://www.googleapis.com/auth/drive");
96
+ public final Scope SCOPE_SHEETS = new Scope("https://www.googleapis.com/auth/spreadsheets");
97
+ public final String[] SCOPES_ARRAYS = {SheetsScopes.DRIVE, SheetsScopes.SPREADSHEETS};
98
+ public final List<String> SCOPES = Arrays.asList(SCOPES_ARRAYS);
99
+ public static final String PREF_ACCOUNT_NAME = "accountName";
100
+ public static final String accountName = "null";
101
+ public GoogleAccountCredential credential;
102
+
103
+ protected void onCreate(Bundle savedInstanceState) {
104
+ super.onCreate(savedInstanceState);
105
+ setContentView(R.layout.activity_main);
106
+
107
+ credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), SCOPES)
108
+ .setBackOff(new ExponentialBackOff());
109
+
110
+ Intent getgsc = credential.newChooseAccountIntent();
111
+
112
+ ActivityResultLauncher<Intent> chooseaccount = registerForActivityResult(
113
+ new ActivityResultContracts.StartActivityForResult(),
114
+ new ActivityResultCallback<ActivityResult>(){
115
+ @Override
116
+ public void onActivityResult(ActivityResult result) {
117
+ if (result.getResultCode() == Activity.RESULT_OK && getgsc.getExtras() != null) {
118
+ String accountName = getgsc.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
119
+ Toast tst_accountName = Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG);
120
+ tst_accountName.show();
121
+ if (accountName != null) {
122
+ SharedPreferences settings =
123
+ getPreferences(Context.MODE_PRIVATE);
124
+ SharedPreferences.Editor editor = settings.edit();
125
+ editor.putString(PREF_ACCOUNT_NAME, accountName);
126
+ editor.apply();
127
+ credential.setSelectedAccountName(accountName);
128
+ }
129
+ } else if (result.getResultCode() == Activity.RESULT_CANCELED) {
130
+ finishAndRemoveTask();
131
+ } else {
132
+ }
133
+ }
134
+ });
135
+
136
+ Button btn = findViewById(R.id.switch1);
137
+ btn.setOnClickListener(new View.OnClickListener(){
138
+ public void onClick(View view){
139
+ chooseaccount.launch(getgsc);
140
+ }
141
+ });
142
+ }
143
+ }
144
+ ```
145
+ ```layout
146
+ <?xml version="1.0" encoding="utf-8"?>
147
+ <LinearLayout
148
+ xmlns:android="http://schemas.android.com/apk/res/android"
149
+ xmlns:tools="http://schemas.android.com/tools"
150
+ xmlns:app="http://schemas.android.com/apk/res-auto"
151
+ android:layout_width="match_parent"
152
+ android:layout_height="match_parent"
153
+ android:orientation="vertical"
154
+ tools:context=".MainActivity">
155
+
156
+ <LinearLayout
157
+ android:orientation="horizontal"
158
+ android:layout_width="match_parent"
159
+ android:layout_height="wrap_content">
160
+ <TextView
161
+ android:text="スイッチ"
162
+ android:layout_width="0dp"
163
+ android:layout_height="wrap_content"
164
+ android:layout_weight="3"
165
+ android:textStyle="bold"
166
+ android:textSize="20sp"/>
167
+ <Button
168
+ android:id="@+id/switch1"
169
+ android:layout_width="0dp"
170
+ android:layout_height="wrap_content"
171
+ android:layout_weight="1.5"
172
+ android:text="スイッチ"/>
173
+ </LinearLayout>
174
+
175
+ </LinearLayout>
176
+ ```
177
+
178
+
44
179
  ### 補足情報
45
180
  Androidバージョン10
46
181
  Android SDK built for x86_64