Android App崩溃的AddAccountExplicitly

时间:2016-05-20 13:46:59

标签: android accountmanager

我在我的应用程序中使用accountmanager。 我首先检查一个帐户,看它是否存在。如果没有,则明确添加。该代码在addaccountexplicitly()

中崩溃
Account[] accounts = AccountManager.get(this).getAccountsByType(getString(R.string.authtype));
        if(accounts.length==1)
        {
            Toast.makeText(this,accounts[0].name,Toast.LENGTH_SHORT).show();
            Intent i = new Intent(Splash.this, MapsActivity.class);
            startActivity(i);
            finish();
        }
        else
        {
            boolean accountCreated = mAccountManager.addAccountExplicitly(new Account("sasa",getString(R.string.authtype)), "", null);

            Intent i = new Intent(Splash.this, MainActivity.class);
            startActivity(i);
            finish();
        }

崩溃日志

    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:224)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                       Caused by: java.lang.SecurityException: uid 10149 cannot explicitly add accounts of type: com.rakebny.musta.authtype
                                                                          at android.os.Parcel.readException(Parcel.java:1627)
                                                                          at android.os.Parcel.readException(Parcel.java:1579)
                                                                          at android.accounts.IAccountManager$Stub$Proxy.addAccountExplicitly(IAccountManager.java:890)
                                                                          at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:722)

1 个答案:

答案 0 :(得分:1)

要使设备处理自定义帐户类型,必须为该帐户类型注册验证器。

获得验证者后,将其声明为清单中的服务:

        <service
            android:name=".MyAuthenticationService"
            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>

然后/res/xml/authenticator.xml会告诉系统您的帐户类型:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/authtype"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher" />

您还需要获得许可

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

以下是网络上用于编码自定义Android身份验证器的绝对最佳文章:Write your own Android Authenticator