Skip to content

Instantly share code, notes, and snippets.

@nic0lette
Created May 25, 2018 23:58
Show Gist options
  • Save nic0lette/f39ab0682272a7cc03504528ec7e1530 to your computer and use it in GitHub Desktop.
Save nic0lette/f39ab0682272a7cc03504528ec7e1530 to your computer and use it in GitHub Desktop.
Example of using AccountManager.newChooseAccountIntent without GET_ACCOUNTS permission
/*
* Copyright 2018 Google LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.accountchoose
import android.accounts.AccountManager
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
class MainActivity : AppCompatActivity() {
private var promptedForAccount = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
if (!promptedForAccount) {
promptedForAccount = true
val intent = AccountManager.newChooseAccountIntent(
null,
null,
arrayOf("com.google"),
false,
null,
null,
null,
null)
startActivityForResult(intent, ACCOUNT_PICK)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ACCOUNT_PICK) {
val accountName = data?.extras?.get(AccountManager.KEY_ACCOUNT_NAME) ?: "NONE"
Log.d("MainActivity", "Selected account: $accountName")
}
}
}
private const val ACCOUNT_PICK = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment