【问题标题】:how to specify account for ContactsContract editor如何为 ContactsContract 编辑器指定帐户
【发布时间】:2013-02-26 01:47:47
【问题描述】:

从我的应用程序中,我想调用联系人编辑器以允许用户添加新联系人。我想找到一种方法让应用程序将要使用的帐户传递给编辑器。如何做到这一点?

我还没有找到有关如何执行此操作的任何文档。我确实在 github 上找到了一些代码,其中显示了一个联系人编辑器正在检索帐户信息(请参阅ContactEditorActivity)。它调用

  getParcelable(Intents.Insert.ACCOUNT);

我认为这一定是不推荐使用的代码,因为我在引用中的任何地方都找不到值 Intents.Insert.ACCOUNT。

总的来说,我调用编辑器的代码是有效的;这是摘录:

  Intent intent = new Intent();
  intent.setAction (Intent.ACTION_INSERT);
  intent.setData (ContactsContract.Contacts.CONTENT_URI);
  intent.putExtra (ContactsContract.Intents.Insert.NAME, name);
  ... "put" other values ...
  startActivityForResult (intent, ACTIVITY_REQUEST_FULL_EDIT);

谢谢。

【问题讨论】:

    标签: android account contactscontract


    【解决方案1】:

    我知道这是一个老问题,但这是我的解决方案,似乎对我有用,以防万一有人遇到同样的问题。

    我在 Android 上使用 Xamarin,但 Java 代码几乎相同。

    对于 API 23+,使用 ExtraAccount 字段 EG:

    Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);
    
    ...
    intent.PutExtra(ContactsContract.Intents.Insert.ExtraAccount, account);
    ...
    

    对于较旧的 API,我使用了“com.android.contacts.extra.ACCOUNT”字段,例如:

    Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);
    
    ...
    intent.PutExtra("com.android.contacts.extra.ACCOUNT", account);
    ...
    

    这打开了联系意图,默认为传入的帐户(绕过任何电话默认值)。这很方便,因为我正在设置一大堆信息,如 url、职位等,但由于联系意图默认为 SIM 卡 - 它无法保存这些数据,所以它丢失了!

    如果您想知道如何在手机上获取帐户,您可以使用:

    AccountManager.Get(context).GetAccountsByType("com.google"); 
    

    获取谷歌帐户,或

    AccountManager.Get(context).GetAccounts(); 
    

    获取所有帐户。但是不包括电话帐户或 SIM 帐户,所以我使用此代码来获取这些:

        using Android.Accounts;
    
    ...
    
        public List<Account> GetAccounts()
        {
                if (CheckReadContactsPermission())
                {
                    var accountList = new List<Account>();
                    var uri = ContactsContract.Settings.ContentUri;
    
                    string[] projection = { ContactsContract.Settings.InterfaceConsts.AccountName,
                                        ContactsContract.Settings.InterfaceConsts.AccountType };
    
                    var loader = new CursorLoader(context, uri, projection, null, null, null);
                    var cursor = (ICursor)loader.LoadInBackground();
    
                    if (cursor.MoveToFirst())
                    {
                        do
                        {
                            accountList.Add(new Account(cursor.GetString(cursor.GetColumnIndex(projection[0])),
                                cursor.GetString(cursor.GetColumnIndex(projection[1]))));
                        } while (cursor.MoveToNext());
                    }
    
                    return accountList;
                }
    
                return null;
            }
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 2022-01-20
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多