小小的努力中
2017-07-15 08:39
采纳率: 50%
浏览 1.8k
已结题

Android AccountManager获取账户为空

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private ListView lv;
AccountManager accountManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    accountManager= (AccountManager) getSystemService(ACCOUNT_SERVICE);
   lv = (ListView) findViewById(R.id.lv);
    Account[] account= accountManager.getAccounts();
    Log.d(TAG,account.length+ "");
    lv.setAdapter(new AbAccountAuthenticator(getBaseContext() ,account));


<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

已经注册了
07-15 08:37:55.297 24730-24730/com.icss.test D/Main
Activity: 0

API
/**
* Lists all accounts of any type registered on the device.
* Equivalent to getAccountsByType(null).
*列出设备上注册的任何类型的所有帐户

 * <p>It is safe to call this method from the main thread.
 *
 * <p>Clients of this method that have not been granted the
 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
 * will only see those accounts managed by AbstractAccountAuthenticators whose
 * signature matches the client.
 *
 * @return An array of {@link Account}, one for each account.  Empty
 *     (never null) if no accounts have been added.
 */
@NonNull
@RequiresPermission(GET_ACCOUNTS)
public Account[] getAccounts() {
    try {
        return mService.getAccounts(null, mContext.getOpPackageName());
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

但是结果怎么会获取为空呢 ,还请指导、小的多谢了。

  • 点赞
  • 收藏

2条回答 默认 最新

  • 小风筝0010 2018-03-23 06:18
    已采纳

    除非身份验证器拥有用户帐号或用户授予访问权限,否则,应用将无法再访问用户帐号。仅拥有 GET_ACCOUNTS 权限尚不足以访问用户帐号。要获得帐号访问权限,应用应使用 AccountManager.newChooseAccountIntent() 或特定于身份验证器的函数。获得帐号访问权限后,应用可以调用 AccountManager.getAccounts() 来访问帐号。

    这是AndroidO 的行为变更,请问您的问题解决了没?

    点赞 打赏 评论
  • devmiao 2017-07-15 13:54
    点赞 打赏 评论

相关推荐 更多相似问题