2

I am trying to make an app with drawer functionality and wanted to set the name, email and profile picture of the user already having a google account on the phone. However, I don't wish the user to ask to sign-in, so I am using the AccountManager.get(this).getAccounts() functionality. This functionality seems to be working when I set targetsdk below 23, but returns an empty array when I set the targetsdk to 26. I know the concept of runtime permissions that are causing this issue, however, I have implemented them as per the google's guide and thus, am even checking if the permission is given or not, if not asking the user for permission, still I get an empty array.

Questions is similar to targetSdkVersion 23 returns 0 length array via accountManager.getAccounts() but the answer there doesnt seem to work for me as I have implemented that itself and the result is the unfortunately same(empty array).

I am a bit of new to this and have done my research but, I failed to find an answer. So, any help would be appreciated.

MainActivity.java(Some Part of Code, remaining is nothing but auto-generated by android studio)

            private static final int MY_PERMISSIONS_REQUEST_GET_ACCOUNTS = 5555;
            private  NavigationView navigationView;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);

                if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED)
                {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.GET_ACCOUNTS))
                    {

                        // Show an explanation to the user *asynchronously* -- don't block
                        // this thread waiting for the user's response! After the user
                        // sees the explanation, try again to request the permission.
                    }
                    else
                    {
                        // No explanation needed, we can request the permission.
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS}, MY_PERMISSIONS_REQUEST_GET_ACCOUNTS);
                    }
                }

                getDetailsForDrawer(navigationView);
            }

            @Override
            public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
                //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                switch(requestCode)
                {
                    case MY_PERMISSIONS_REQUEST_GET_ACCOUNTS:
                        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                        {
                            getDetailsForDrawer(navigationView);
                        }
                        else
                        {
                            //set default img name n email id
                        }
                        break;
                }
            }

            private  void getDetailsForDrawer(NavigationView navigationView)
            {
                //AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
                //Account[] list = manager.getAccounts();
                Account[] list = AccountManager.get(this).getAccounts();
                String gmail = null;

                for (Account account : list) {
                    if (account.type.equalsIgnoreCase("com.google")) {
                        gmail = account.name;
                        break;
                    }
                }


                NavHeader nv = new NavHeader();
                nv.setProfileInDrawer(navigationView.getHeaderView(0));
            }

NavHeader.java(Just for testing)

public void setProfileInDrawer(View view)
{
    imageView = (ImageView) view.findViewById(R.id.profileImage);
    textView1 = (TextView) view.findViewById(R.id.userName);
    textView2 = (TextView) view.findViewById(R.id.userEmail);

    /*
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().requestProfile().build();

    GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount((Activity) view.getContext());
    if (acct != null) {
        String personName = acct.getDisplayName();
        String personGivenName = acct.getGivenName();
        String personFamilyName = acct.getFamilyName();
        String personEmail = acct.getEmail();
        String personId = acct.getId();
        Uri personPhoto = acct.getPhotoUrl();
        imageView.setImageURI(personPhoto);
        textView1.setText(personName);
        textView2.setText(personEmail); 
    }*/
textView1.setText("Testing");
 }

Android-Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxx">

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<application
.
.
.>
</application>
</manifest>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Harshad Loya
  • 123
  • 1
  • 2
  • 13

1 Answers1

6

From Android 8.0, GET_ACCOUNTS is no longer sufficient to access the Accounts on device.

Based on the documentation:

In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.

You can check this SO for usage of AccountManager.newChooseAccountIntent()

Example: ![This will start an activity for user and display list of google accounts. Upon user selection override method @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) where in you can retrieve the user selected account String selectedGmailAccount = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) ]3

MG Developer
  • 859
  • 11
  • 17
Sagar
  • 23,903
  • 4
  • 62
  • 62