首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android SecurityException: uid不能显式添加帐户

Android SecurityException: uid不能显式添加帐户
EN

Stack Overflow用户
提问于 2019-07-09 08:14:15
回答 1查看 4.1K关注 0票数 5

我收到错误消息

java.lang.SecurityException: uid 10178 cannot explicitly add accounts of type: net.roughdesign.swms

即使是我能创建的最基本的例子。它包括:

帐户配置

帐户类型在strings.xml

代码语言:javascript
运行
复制
<string name="accounts__account_type">net.roughdesign.swms</string>

authenticator.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
            android:accountType="@string/accounts__account_type"
            android:icon="@drawable/ic_add_black_24dp"
            android:smallIcon="@drawable/ic_add_black_24dp"
            android:label="@string/global__authenticator_account_label"
            />

</resources>

SwmsAccountAuthenticator.kt

代码语言:javascript
运行
复制
class SwmsAccountAuthenticator(val context: Context) : AbstractAccountAuthenticator(context) {

    override fun addAccount(
        response: AccountAuthenticatorResponse, accountType: String, authTokenType: String?,
        requiredFeatures: Array<out String>?, options: Bundle?
    ): Bundle? {
        return null
    }


    override fun confirmCredentials(response: AccountAuthenticatorResponse, account: Account, options: Bundle?)
            : Bundle? {
        return null
    }


    override fun editProperties(response: AccountAuthenticatorResponse?, accountType: String?): Bundle? {
        return null
    }


    override fun getAuthToken(
        response: AccountAuthenticatorResponse, accountInput: Account, authTokenType: String, options: Bundle?
    ): Bundle? {

        return null
    }


    override fun getAuthTokenLabel(authTokenType: String): String? {
        return null
    }


    override fun hasFeatures(response: AccountAuthenticatorResponse, account: Account, features: Array<out String>)
            : Bundle? {
        return null
    }


    override fun updateCredentials(
        response: AccountAuthenticatorResponse, account: Account, authTokenType: String?, options: Bundle?
    ): Bundle? {
        return null
    }
}

SwmsAuthenticatorService.kt

代码语言:javascript
运行
复制
class SwmsAuthenticatorService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        val authenticator = SwmsAccountAuthenticator(this)
        return authenticator.iBinder
    }
}

,最后,在AndroidManifest.xml

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application ... >

    <service
            android:name=".users.SwmsAuthenticatorService"
            android:exported="false">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
    </service>

   ...
</application>

试着用它

我试图用以下内容创建一个帐户:

代码语言:javascript
运行
复制
val username = "myUserName"
val password = "myPassword"

val accountManager = AccountManager.get(this)
val accountType = getString(R.string.accounts__account_type)
val account = Account(username, accountType)

val accountAdded = accountManager.addAccountExplicitly(account, password, null)

然后,对于最后一行(accountManager.addAccountExplicitly),我收到前面提到的错误:

代码语言:javascript
运行
复制
   java.lang.SecurityException: uid 10178 cannot explicitly add accounts of type: net.roughdesign.swms
        at android.os.Parcel.readException(Parcel.java:2013)
        at android.os.Parcel.readException(Parcel.java:1959)
        at android.accounts.IAccountManager$Stub$Proxy.addAccountExplicitly(IAccountManager.java:1205)
        at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:878)
        at net.roughdesign.swms.swmsandroid.users.AuthenticateActivity.submit(AuthenticateActivity.kt:68)
...

我完全不知道这里还会有什么问题。据我所知,帐户类型(net.roughdesign.swms)是通过在清单中添加<service>部分而在我的应用程序中“注册”的,但我认为安卓不接受“它是我的”这一说法。

这里还有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-09 21:49:00

通用解决方案--如何调试

在android的logcat视图中,默认筛选器设置为“只显示选定的应用程序”。我确实收到了关于我的帐户服务无法注册的日志,包括原因。但是,对于默认的筛选器设置,它们没有显示!

因此,为了了解这里到底发生了什么,请将过滤器设置切换到"No“。这将导致出现问题的错误消息。

但是,它会显示所有的日志消息,因此您将得到大量的日志消息。我不得不滚动相当多的时间才能看到错误信息。我的建议是,在得到所需的内容后,将过滤器返回到“只显示选定的应用程序”。

我的具体问题

问题在于authenticator.xml。这个文件不应该有一个<resources>元素。<account-authenticator>需要是一个顶级元素;完整的文件需要如下所示:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
        android:accountType="@string/accounts__account_type"
        android:icon="@drawable/ic_add_black_24dp"
        android:label="@string/global__authenticator_account_label"
        android:smallIcon="@drawable/ic_add_black_24dp" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56948158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档