我们有个方法,是判断系统的账号有没有登录。

public static boolean isAccountLogin(Context context) {
String df = "com.z**;
AccountManager accountManager = AccountManager.get(context); try {
Account[] accounts = accountManager.getAccountsByType(df);
if (accounts != null && accounts.length > 0) {
Account account = accounts[0];
SDKLogUtils.d("PackageUtils", account.toString());
if (account != null) {
return true;
}
} else {
SDKLogUtils.d("PackageUtils", "com.ztemt account is null");
}
} catch (Exception var5) {
var5.printStackTrace();
} return false;
}

这个在8.0以前的手机,完全没问题。但是8.0就有问题。而且改一下target 版本到8.0才有问题。

赶紧查一下。果然8.0变更了。https://developer.android.com/about/versions/oreo/android-8.0-changes.html

Apps targeting Android 8.0 这个是target 提到8.0以后才需要注意的

These behavior changes apply exclusively to apps that are targeting Android 8.0 (API level 26) or higher. Apps that compile against Android 8.0, or set targetSdkVersion to Android 8.0 or higher must modify their apps to support these behaviors properly, where applicable to the app.

Account access and discoverability

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.

Android 8.0 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime.

For information about new APIs and methods added for account access and discoverability, see Account Access and Discoverability in the New APIs section of this document.

OK,下载下来8.0的代码,看下

    public Account[] getAccountsByType(String type) {
return getAccountsByTypeAsUser(type, Process.myUserHandle());
}
    /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
@NonNull
public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
try {
return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
mContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
    private final IAccountManager mService;

这个Service 是个啥?

我也不知道,用SourceInsight 全局搜一下getAccountsAsUser 这个方法的实现吧。好吧,没收到。

看了别人的,说是:AccountManagerService,可能是sdk 源码不全

OK,继续

最终会调用到这里:


/**
* Method which handles default values for Account visibility.
*
* @param account The account to check visibility.
* @param packageName Package name to check visibility
* @param accounts UserAccount that currently hosts the account and application
*
* @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED
*
*/
private Integer resolveAccountVisibility(Account account, @NonNull String packageName,
UserAccounts accounts) {
Preconditions.checkNotNull(packageName, "packageName cannot be null");
int uid = -1;
try {
long identityToken = clearCallingIdentity();
try {
uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId);
} finally {
restoreCallingIdentity(identityToken);
}
} catch (NameNotFoundException e) {
Log.d(TAG, "Package not found " + e.getMessage());
return AccountManager.VISIBILITY_NOT_VISIBLE;
} // System visibility can not be restricted.
if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) {
return AccountManager.VISIBILITY_VISIBLE;
} int signatureCheckResult =
checkPackageSignature(account.type, uid, accounts.userId); // Authenticator can not restrict visibility to itself.
if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) {
return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account
} // Return stored value if it was set.
int visibility = getAccountVisibilityFromCache(account, packageName, accounts); if (AccountManager.VISIBILITY_UNDEFINED != visibility) {
return visibility;
} boolean isPrivileged = isPermittedForPackage(packageName, uid, accounts.userId,
Manifest.permission.GET_ACCOUNTS_PRIVILEGED); // Device/Profile owner gets visibility by default.
if (isProfileOwner(uid)) {
return AccountManager.VISIBILITY_VISIBLE;
} boolean preO = isPreOApplication(packageName);
if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH)
|| (preO && checkGetAccountsPermission(packageName, uid, accounts.userId))
|| (checkReadContactsPermission(packageName, uid, accounts.userId)
&& accountTypeManagesContacts(account.type, accounts.userId))
|| isPrivileged) {
// Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature
// match.
visibility = getAccountVisibilityFromCache(account,
AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts);
if (AccountManager.VISIBILITY_UNDEFINED == visibility) {
visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE;
}
} else {
visibility = getAccountVisibilityFromCache(account,
AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts);
if (AccountManager.VISIBILITY_UNDEFINED == visibility) {
visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE;
}
}
return visibility;
}

只有这里返回对你可见,那么你才能访问账户。

1.signatureCheckResult != SIGNATURE_CHECK_MISMATCH 同签名

2.preO && checkGetAccountsPermission(packageName, uid, accounts.userId)

target 大于android O 并且有GetAccounts 权限

3.checkReadContactsPermission(packageName, uid, accounts.userId)

&& accountTypeManagesContacts(account.type, accounts.userId)

当前的包名有 ReadContacts 权限,并且 授予账号的包,有Manifest.permission.WRITE_CONTACTS的权限

4. 享有特权的, 即 有GET_ACCOUNTS_PRIVILEGED权限

总结:

1.源码大概了解意思就行,不必每个东西都弄懂。

2.一遍看不懂,或者没有看到关键,多看几遍。

3.使用SourceInSight

4.要时时关注最新的动态,不要改了东西,自己不知道。遇到问题了,去百度。看别人的博客。才知道怎么解决。

参考:

https://blog.csdn.net/gdutxiaoxu/article/details/80099717

android 8.0 Account行为变更 账号系统的更多相关文章

  1. Android 4.0 事件输入(Event Input)系统

    参考:http://blog.csdn.net/myarrow/article/details/7091061 1. TouchScreen功能在Android4.0下不工作 原来在Android2. ...

  2. android 8.0变更

    Android 8.0 行为变更 Android 8.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 其中大部分变 ...

  3. Android 6.0权限全面详细分析和解决方案

    原文: http://www.2cto.com/kf/201512/455888.html http://blog.csdn.net/yangqingqo/article/details/483711 ...

  4. Android 6.0 Permission权限与安全机制

    Marshmallow版本权限修改 android的权限系统一直是首要的安全概念,因为这些权限只在安装的时候被询问一次.一旦安装了,app可以在用户毫不知晓的情况下访问权限内的所有东西,而且一般用户安 ...

  5. Android 9.0更新

    北京时间2018年8月7日上午,Google 发布了 Android 9.0 操作系统.并宣布系统版本 Android P 被正式命名为代号"Pie". Android 9.0 利 ...

  6. Android 7.0 新增功能和api

    Android 7.0 Nougat 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 请务必查阅 Android 7.0 行为变更以了解平台变更可能影响您的应用的领域. 要详细了解 A ...

  7. Android 6.0权限

    在android 6.0 Marshmallow版本之后,系统不会在软件安装的时候就赋予该app所有其申请的权限,对于一些危险级别的权限,app需要在运行时一个一个询问用户授予权限. 只有那些targ ...

  8. android studio2.0出现的gradle问题,instant Run即时运行不了.

    android studio 2.0出现的gradle问题: instant Run即时运行不了.经历了几乎9个preView版本的AS2.0,终于迎来了正式版,然而晴天我的霹雳,好不容易装好的2.0 ...

  9. Nexus5 电信3G保留数据和Root升级Android 6.0

    前提: A 备份手机重要数据,安全第一 B 进入twrp recovery 备份EFS,建议最好拷贝到电脑上(如果没有twrp,则需要先刷twrp,具体指令请看下面步骤第10条) C 因为Androi ...

随机推荐

  1. 关于一篇对epoll讲的比较好的一篇文章

    原文地址http://www.cnblogs.com/lojunren/p/3856290.html 前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是select和poll,自Li ...

  2. poj1322 Chocolate 【 概率DP 】

    题目链接:poj1322 Chocolate [概率DP ] 题意:袋中有C种颜色巧克力,每次从其中拿出一块放桌上,如果桌上有两块相同颜色巧克力则吃掉,问取出N块巧克力后,求桌上正好剩下M块巧克力的概 ...

  3. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  4. MATLAB入门学习(五)

    现在,我们来学画图吧.╭( ・ㅂ・)و ̑̑ 绘制函数图像最常用的命令是plot plot(x,y,s)x,y为同维向量,绘制分别以x为横坐标,y为纵坐标的曲线 如果x y 是矩阵的话则会绘制多条曲线 ...

  5. Failed to read candidate component错误

    <context:component-scan base-package="com.spdb"/> <mvc:annotation-driven/> 加上该 ...

  6. Codeforces 955C - Sad powers(数论 + 二分)

    链接: http://codeforces.com/problemset/problem/955/C 题意: Q次询问(1≤Q≤1e5),每次询问给出两个整数L, R(1≤L≤R≤1e18),求所有符 ...

  7. react 开发中的问题简记

    1.什么时候用props 什么时候用state ? 不能使用props:当页面组件存在URL跳转问题时候,原因:若单独刷新,他会报错,拿不到前面的数据: 使用props场景:当组件为页面组件的一部分即 ...

  8. Cocos2dx打包apk时变更NDK引发问题及解决

    现在官方的Cocos Studio已经支持打包apk文件,写该随笔的时候还没试过官方的打包功能,所以就按自己的学习顺序先把打包的心得写下. 问题及最终解决方案: 其中耗时最长的问题就是ndk-r10改 ...

  9. 如何将编写好的CS文件做成exe可执行文件

    编译好的控制台CS文件: cs文件单独拿出来放到F盘目录中 打开命令提示CMD: 执行下面语句即可在F盘目录中看到生成的exe文件,windows系统下双击就可运行: C:\Windows\Micro ...

  10. iview中table里嵌套i-switch、input、select等

    iview中table内嵌套 input render:(h,params) => { return h('Input',{ props: { value:'', size:'small', } ...