質問編集履歴
1
詳細コードの明示
test
CHANGED
File without changes
|
test
CHANGED
@@ -80,6 +80,276 @@
|
|
80
80
|
|
81
81
|
```
|
82
82
|
|
83
|
+
###詳細コード
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
```MainActivity
|
88
|
+
|
89
|
+
package com.example.test;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
import android.Manifest;
|
94
|
+
|
95
|
+
import androidx.appcompat.app.AppCompatActivity;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
import android.app.Activity;
|
100
|
+
|
101
|
+
import android.os.Bundle;
|
102
|
+
|
103
|
+
import android.app.Dialog;
|
104
|
+
|
105
|
+
import android.content.Context;
|
106
|
+
|
107
|
+
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
|
108
|
+
|
109
|
+
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
110
|
+
|
111
|
+
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
112
|
+
|
113
|
+
import com.google.android.gms.common.api.Scope;
|
114
|
+
|
115
|
+
import android.content.Intent;
|
116
|
+
|
117
|
+
import androidx.activity.result.ActivityResultCaller;
|
118
|
+
|
119
|
+
import androidx.activity.result.ActivityResultLauncher;
|
120
|
+
|
121
|
+
import androidx.activity.result.ActivityResultCallback;
|
122
|
+
|
123
|
+
import androidx.activity.result.contract.ActivityResultContracts;
|
124
|
+
|
125
|
+
import androidx.activity.result.ActivityResult;
|
126
|
+
|
127
|
+
import androidx.biometric.BiometricManager.Authenticators;
|
128
|
+
|
129
|
+
import android.provider.Settings;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
import com.google.api.client.util.ExponentialBackOff;
|
134
|
+
|
135
|
+
import com.google.api.services.sheets.v4.Sheets;
|
136
|
+
|
137
|
+
import com.google.api.services.sheets.v4.model.*;
|
138
|
+
|
139
|
+
import com.google.api.services.sheets.v4.SheetsScopes;
|
140
|
+
|
141
|
+
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
142
|
+
|
143
|
+
import android.accounts.AccountManager;
|
144
|
+
|
145
|
+
import android.content.SharedPreferences;
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
import com.google.android.gms.common.AccountPicker;
|
150
|
+
|
151
|
+
import com.google.android.gms.common.AccountPicker.AccountChooserOptions;
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
//Debug
|
156
|
+
|
157
|
+
import android.widget.Button;
|
158
|
+
|
159
|
+
import android.view.View;
|
160
|
+
|
161
|
+
import android.widget.Toast;
|
162
|
+
|
163
|
+
import android.util.Log;
|
164
|
+
|
165
|
+
import java.util.List;
|
166
|
+
|
167
|
+
import java.util.Arrays;
|
168
|
+
|
169
|
+
import java.io.IOException;
|
170
|
+
|
171
|
+
import java.util.ArrayList;
|
172
|
+
|
173
|
+
import pub.devrel.easypermissions.AfterPermissionGranted;
|
174
|
+
|
175
|
+
import pub.devrel.easypermissions.EasyPermissions;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
public class MainActivity extends AppCompatActivity
|
180
|
+
|
181
|
+
implements Authenticators{
|
182
|
+
|
183
|
+
public final int REQUEST_DRIVE = 1;
|
184
|
+
|
185
|
+
public final int REQUEST_SHEETS = 10;
|
186
|
+
|
187
|
+
public final int REQUEST_CODE= 100;
|
188
|
+
|
189
|
+
public final Scope SCOPE_DRIVE = new Scope("https://www.googleapis.com/auth/drive");
|
190
|
+
|
191
|
+
public final Scope SCOPE_SHEETS = new Scope("https://www.googleapis.com/auth/spreadsheets");
|
192
|
+
|
193
|
+
public final String[] SCOPES_ARRAYS = {SheetsScopes.DRIVE, SheetsScopes.SPREADSHEETS};
|
194
|
+
|
195
|
+
public final List<String> SCOPES = Arrays.asList(SCOPES_ARRAYS);
|
196
|
+
|
197
|
+
public static final String PREF_ACCOUNT_NAME = "accountName";
|
198
|
+
|
199
|
+
public static final String accountName = "null";
|
200
|
+
|
201
|
+
public GoogleAccountCredential credential;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
protected void onCreate(Bundle savedInstanceState) {
|
206
|
+
|
207
|
+
super.onCreate(savedInstanceState);
|
208
|
+
|
209
|
+
setContentView(R.layout.activity_main);
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), SCOPES)
|
214
|
+
|
215
|
+
.setBackOff(new ExponentialBackOff());
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
Intent getgsc = credential.newChooseAccountIntent();
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
ActivityResultLauncher<Intent> chooseaccount = registerForActivityResult(
|
224
|
+
|
225
|
+
new ActivityResultContracts.StartActivityForResult(),
|
226
|
+
|
227
|
+
new ActivityResultCallback<ActivityResult>(){
|
228
|
+
|
229
|
+
@Override
|
230
|
+
|
231
|
+
public void onActivityResult(ActivityResult result) {
|
232
|
+
|
233
|
+
if (result.getResultCode() == Activity.RESULT_OK && getgsc.getExtras() != null) {
|
234
|
+
|
235
|
+
String accountName = getgsc.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
|
236
|
+
|
237
|
+
Toast tst_accountName = Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG);
|
238
|
+
|
239
|
+
tst_accountName.show();
|
240
|
+
|
241
|
+
if (accountName != null) {
|
242
|
+
|
243
|
+
SharedPreferences settings =
|
244
|
+
|
245
|
+
getPreferences(Context.MODE_PRIVATE);
|
246
|
+
|
247
|
+
SharedPreferences.Editor editor = settings.edit();
|
248
|
+
|
249
|
+
editor.putString(PREF_ACCOUNT_NAME, accountName);
|
250
|
+
|
251
|
+
editor.apply();
|
252
|
+
|
253
|
+
credential.setSelectedAccountName(accountName);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
} else if (result.getResultCode() == Activity.RESULT_CANCELED) {
|
258
|
+
|
259
|
+
finishAndRemoveTask();
|
260
|
+
|
261
|
+
} else {
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
});
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
Button btn = findViewById(R.id.switch1);
|
272
|
+
|
273
|
+
btn.setOnClickListener(new View.OnClickListener(){
|
274
|
+
|
275
|
+
public void onClick(View view){
|
276
|
+
|
277
|
+
chooseaccount.launch(getgsc);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
});
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
```layout
|
290
|
+
|
291
|
+
<?xml version="1.0" encoding="utf-8"?>
|
292
|
+
|
293
|
+
<LinearLayout
|
294
|
+
|
295
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
296
|
+
|
297
|
+
xmlns:tools="http://schemas.android.com/tools"
|
298
|
+
|
299
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
300
|
+
|
301
|
+
android:layout_width="match_parent"
|
302
|
+
|
303
|
+
android:layout_height="match_parent"
|
304
|
+
|
305
|
+
android:orientation="vertical"
|
306
|
+
|
307
|
+
tools:context=".MainActivity">
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
<LinearLayout
|
312
|
+
|
313
|
+
android:orientation="horizontal"
|
314
|
+
|
315
|
+
android:layout_width="match_parent"
|
316
|
+
|
317
|
+
android:layout_height="wrap_content">
|
318
|
+
|
319
|
+
<TextView
|
320
|
+
|
321
|
+
android:text="スイッチ"
|
322
|
+
|
323
|
+
android:layout_width="0dp"
|
324
|
+
|
325
|
+
android:layout_height="wrap_content"
|
326
|
+
|
327
|
+
android:layout_weight="3"
|
328
|
+
|
329
|
+
android:textStyle="bold"
|
330
|
+
|
331
|
+
android:textSize="20sp"/>
|
332
|
+
|
333
|
+
<Button
|
334
|
+
|
335
|
+
android:id="@+id/switch1"
|
336
|
+
|
337
|
+
android:layout_width="0dp"
|
338
|
+
|
339
|
+
android:layout_height="wrap_content"
|
340
|
+
|
341
|
+
android:layout_weight="1.5"
|
342
|
+
|
343
|
+
android:text="スイッチ"/>
|
344
|
+
|
345
|
+
</LinearLayout>
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
</LinearLayout>
|
350
|
+
|
351
|
+
```
|
352
|
+
|
83
353
|
|
84
354
|
|
85
355
|
|