gpt4 book ai didi

java - AccountManager.getAccounts() 在 targetsdk > 23 中不起作用

转载 作者:行者123 更新时间:2023-11-29 08:29:00 27 4
gpt4 key购买 nike

我正在尝试制作一个具有抽屉功能的应用程序,并想设置手机上已经拥有谷歌帐户的用户的姓名、电子邮件和个人资料图片。但是,我不希望用户要求登录,所以我使用了 AccountManager.get(this).getAccounts() 功能。当我将 targetsdk 设置为 23 以下时,此功能似乎有效,但当我将 targetsdk 设置为 26 时返回一个空数组。我知道导致此问题的运行时权限的概念,但是,我已经按照谷歌的指南实现了它们因此,我什至会检查是否已授予权限,如果不询问用户权限,我仍然会得到一个空数组。

问题类似于 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>

最佳答案

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

基于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.

你可以查看这个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 accountString selectedGmailAccount = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) ] 3

关于java - AccountManager.getAccounts() 在 targetsdk > 23 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com