I am working with google account and i want to remove all the logged in account from device with the help of my application and i did it with this line of code
private void signOutGmail() {
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
if (accounts.length > 0) {
Account accountToRemove = accounts[0];
am.removeAccount(accountToRemove, null, null);
}
}
But when i run my application in marshmallow i got an error of Security Exception and my application get crashed is there any solution to remove gmail accounts from device programmatically in marshmallow and up.
This is my exception:
02-27 12:25:11.386 14485-14485/com.example.deletedemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.deletedemo, PID: 14485
java.lang.SecurityException: uid 10145 cannot remove accounts of type: com.google
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.accounts.IAccountManager$Stub$Proxy.removeAccount(IAccountManager.java:915)
at android.accounts.AccountManager$6.doWork(AccountManager.java:844)
at android.accounts.AccountManager$BaseFutureTask.startTask(AccountManager.java:2024)
at android.accounts.AccountManager$Future2Task.start(AccountManager.java:2079)
at android.accounts.AccountManager.removeAccount(AccountManager.java:841)
at com.example.deletedemo.MainActivity.signOutGmail(MainActivity.java:134)
at com.example.deletedemo.MainActivity.onClick(MainActivity.java:68)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
Please Help! Thank You!!