AccountManager.getAccounts()在targetsdk>中无效23

时间:2018-04-14 08:24:09

标签: java android

我正在尝试制作具有抽屉功能的应用,并希望设置已在手机上拥有Google帐户的用户的姓名,电子邮件和个人资料照片。但是,我不希望用户要求登录,因此我使用的是AccountManager.get(this).getAccounts()功能。当我将targetsdk设置为低于23时,此功能似乎正在工作,但是当我将targetsdk设置为26时返回一个空数组。我知道导致此问题的运行时权限的概念,但是,我已按照google&#实现了它们39; s指南,因此,甚至检查是否给予了许可,如果没有要求用户许可,我仍然得到一个空数组。

问题类似于targetSdkVersion 23通过accountManager.getAccounts()返回0长度数组但是答案似乎对我不起作用,因为我已经实现了它自己,结果是不幸的是(空数组)。

我对此有点新意并完成了我的研究但是,我没有找到答案。所以,任何帮助将不胜感激。

MainActivity.java(代码的某些部分,剩下的只不过是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(仅供测试)

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>

1 个答案:

答案 0 :(得分:4)

从Android 8.0开始,GET_ACCOUNTS不再足以访问设备上的帐户。

基于documentation

  

在Android 8.0(API级别26)中,除非身份验证者拥有帐户或用户授予该访问权限,否则应用无法再访问用户帐户。 GET_ACCOUNTS权限不再足够。要授予帐户访问权限,应该使用AccountManager.newChooseAccountIntent()或特定于身份验证器的方法。访问帐户后,应用程序可以调用AccountManager.getAccounts()来访问它们。

您可以查看此SO以了解AccountManager.newChooseAccountIntent()

的使用情况

示例:![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