Intention to go to the settings page for a specific account

With the following well-known code, the user goes to the general Android settings page for accounts on the device:

startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS)); 

sync settings

Is there an equivalent that takes the user directly to the settings for a particular account (see screenshot below) if the account belongs to my application?

specific account sync settings

+6
source share
1 answer

if the account belongs to my application

This, I think, you mean that you know the type of account in advance. If so, then the following is one of the possible approaches to solving the problem.

First, the application will need GET_ACCOUNTS permission.

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

Then, if you look at the onActivityCreated() AccountSyncSettings (action in the second screenshot), you will notice that it is looking for the "account" key in the set of launch intentions.

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle arguments = getArguments(); if (arguments == null) { Log.e(TAG, "No arguments provided when starting intent. ACCOUNT_KEY needed."); finish(); return; } mAccount = (Account) arguments.getParcelable(ACCOUNT_KEY); if (!accountExists(mAccount)) { Log.e(TAG, "Account provided does not exist: " + mAccount); finish(); return; } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Got account: " + mAccount); } mUserId.setText(mAccount.name); mProviderId.setText(mAccount.type); } 

Now, based on this, you can use something as follows to run this action for a specific account:

 private static final String ACCOUNT_KEY = "account"; private static final String ACTION_ACCOUNT_SYNC_SETTINGS = "android.settings.ACCOUNT_SYNC_SETTINGS"; ... // lots of code Account myAccount = null; AccountManager accountManager = AccountManager.get(getApplicationContext()); Account[] accounts = accountManager.getAccounts(); for (Account account : accounts) { if ("com.myTLD.myApp".equals(account.type)) { myAccount = account; break; } } if (myAccount != null) { Bundle args = new Bundle(); args.putParcelable(ACCOUNT_KEY, myAccount); Intent intent = new Intent(ACTION_ACCOUNT_SYNC_SETTINGS); intent.putExtras(args); startActivity(intent); } 

Here, however, several things need to be considered:

  • AccountSyncSettings implementation can be changed at any time.
  • The above code has not been widely tested. In fact, it was tested on only one target device (Galaxy S5 with Lollipop).
  • "android.settings.ACCOUNT_SYNC_SETTINGS" action may not be available on all Android devices and versions. Thus, protect against possible failures and find alternative actions.
  • Instead of getAccounts() you really can use getAccountsByType("com.myTLD.myApp") and just use the first element from the returned array if the target user cannot have more than one account on the device.
  • If you know the name of the account , you can use this information to further customize the if clause to suit your needs.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/987918/


All Articles