我正在使用此代码创建帐户。但它会引发安全异常。并且该帐户不会被创建。
public static Account getSyncAccount(Context context) {
AccountManager accountManager =(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type));
if ( null == accountManager.getPassword(newAccount) ) {
if (!accountManager.addAccountExplicitly(newAccount, "", null))
return null;
onAccountCreated(newAccount, context);
}
return newAccount;
}
logcat 中的错误如下所示:
02-24 22:00:03.722: E/AndroidRuntime(30319): FATAL EXCEPTION: main
02-24 22:00:03.722: E/AndroidRuntime(30319): Process: com.example.yogesh.smartshoppee, PID: 30319
02-24 22:00:03.722: E/AndroidRuntime(30319): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yogesh.smartshoppee/com.example.yogesh.smartshoppee.Activity.ShoppingListActivity}: java.lang.SecurityException: uid 10176 cannot get secrets for accounts of type: Save image
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread.-wrap11(ActivityThread.java)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.os.Handler.dispatchMessage(Handler.java:102)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.os.Looper.loop(Looper.java:148)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread.main(ActivityThread.java:5443)
02-24 22:00:03.722: E/AndroidRuntime(30319): at java.lang.reflect.Method.invoke(Native Method)
02-24 22:00:03.722: E/AndroidRuntime(30319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
02-24 22:00:03.722: E/AndroidRuntime(30319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-24 22:00:03.722: E/AndroidRuntime(30319): Caused by: java.lang.SecurityException: uid 10176 cannot get secrets for accounts of type: Save image
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.os.Parcel.readException(Parcel.java:1620)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.os.Parcel.readException(Parcel.java:1573)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.accounts.IAccountManager$Stub$Proxy.getPassword(IAccountManager.java:694)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.accounts.AccountManager.getPassword(AccountManager.java:334)
02-24 22:00:03.722: E/AndroidRuntime(30319): at com.example.yogesh.smartshoppee.sync.AppSyncAdapter.getSyncAccount(AppSyncAdapter.java:190)
02-24 22:00:03.722: E/AndroidRuntime(30319): at com.example.yogesh.smartshoppee.sync.AppSyncAdapter.initializeSyncAdapter(AppSyncAdapter.java:234)
02-24 22:00:03.722: E/AndroidRuntime(30319): at com.example.yogesh.smartshoppee.Activity.ShoppingListActivity.onCreate(ShoppingListActivity.java:58)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.Activity.performCreate(Activity.java:6245)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
02-24 22:00:03.722: E/AndroidRuntime(30319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
02-24 22:00:03.722: E/AndroidRuntime(30319): ... 9 more
AndroidManifest 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity.LoginScreen1Activity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Activity.MyAccountActivity"
android:label="@string/title_activity_my_account" >
</activity>
<service
android:name="com.example.demo.sync.AppAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<service
android:name="com.example.demo.sync.AppSyncService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter"/>
</intent-filter>
<meta-data android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
</application>
我在网上查了一下,但找不到解决方案。我究竟做错了什么?