2017-12-08 24 views
0

私のアプリはAndroidの連絡先の中に特殊なSYNC4キーを使用して連絡先を作成します。ただし、HTC 10デバイスの連絡先を読むことができません。 HTCデバイスと他のAndroidデバイスとの連絡先の処理に関するいくつかの違いについては読んだことがありますが、残念ながらこの問題の解決策や説明は見つかりませんでした。HTC 10でcontentResolverを使用してAndroidの連絡先を取得すると結果が返されません。

接触の作成は次のように動作:

private static final String SPECIAL_CONTACT_KEY = "_MY_SPECIAL_CONTACT_KEY_"; 

// Create an operation with the special SYN4 key 
ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI); 
     builder.withValue(RawContacts.ACCOUNT_TYPE, null); 
     builder.withValue(RawContacts.ACCOUNT_NAME, null); 
     builder.withValue(RawContacts.SYNC4, SPECIAL_CONTACT_KEY); 
     ops.add(builder.build()); 

// Add a 'given name' to the list of operations 
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI); 
     builder.withValueBackReference(Data.RAW_CONTACT_ID, 0); 
     builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); 
     builder.withValue(StructuredName.GIVEN_NAME, "givenName"); 
     ops.add(builder.build()); 

// Add a 'phone number with type work' to the list of operations 
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI); 
     builder.withValueBackReference(Data.RAW_CONTACT_ID, 0); 
     builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); 
     builder.withValue(Phone.TYPE, Phone.TYPE_WORK); 
     builder.withValue(Phone.NUMBER, "phoneNumber"); 
     ops.add(builder.build()); 

// Add a 'note' to the list of operations (to be able to visually discern the contact from other contacts) 
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI); 
     builder.withValueBackReference(Data.RAW_CONTACT_ID, 0); 
     builder.withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE); 
     builder.withValue(Note.NOTE, "note"); 
     ops.add(builder.build()); 

ContentProviderResult[] result = App.getContext().getContentResolver().applyBatch(AUTHORITY, ops); 

接点(S)は次のように取り出すことができる。

// Retrieve our own contacts using the special SYNC4 key 
Uri uri = Phone.CONTENT_URI; 
String[] projection = {Phone.NUMBER}; 
String mySpecialSelection = RawContacts.SYNC4 + "='" + SPECIAL_CONTACT_KEY + "'"; 
Cursor cursor = App.getContext().getContentResolver().query(uri, projection, mySpecialSelection, null, null); 

カーソルは、すべてのデバイス上の正しい連絡先データで満たされていますHTCではカーソルは空です。

+0

なぜあなたは 'SYNC4'を使用していますか? – pskink

+0

正直言って、それはすでに同僚によって使用されていた、私はそれが他の人との接触を識別する特定のマーカーだと言われました。私はそれが何であるかはわかりませんが、よりよいマーカーのアドバイスがあれば、;-) あなたはそれがHTCの問題に関連していると思いますか? –

答えて

1

ヌルACCOUNT_NAMEACCOUNT_TYPEの連絡先を作成することはできません。これらはゾンビ連絡先と呼ばれ、HTCデバイスだけでなく、デバイスに残っているという保証はありません。

新しい連絡先を作成する必要がある場合は、アプリのカスタムアカウントを作成し、そのアカウントで新しい連絡先/生の連絡先を作成する必要があります。

thisおよびthisを参照してください。 ところで、あなたはSyncAdapterを持っている必要はありませんが、あなたの連絡先のデータの更新が楽になり、Androidのエコシステムでうまくいきます。

RawContactsは、お問い合わせにACCOUNT_TYPE = <your custom type>と入力して検索することができます。

+0

それは問題を解決しました、ありがとう!しかし、リンクの記述は非常に拡張されていました(ユーザーなどがログインするオプションを含む)。私は下の別個の答えで私の解決策を説明します。 –

0

the answer of marmorに基づいて、リンクに記載されているアカウント作成を簡略化しました。私は自分のユーザーがアカウント/ログインなどを作成する必要はありませんが、アカウントを自分で作成するだけです。 1つの連絡先、それは私が必要なすべてです。 は、ここに私のコードの簡易版です。

私はこのようなMyAppAccountManagerクラス作っ:私は連絡先を作成するときに、私はもうSYNC4 SPECIAL_CONTACT_KEYを使用していないが、アカウント(タイプ&

public class MyAppAccountManager 
{ 
    public boolean createMyAppAccountIfNotExists(Context context) 
    { 
     return doesMyAppAccountExist(context) || createNewMyAppAccount(context); 
    } 

    public boolean createNewMyAppAccount(Context context) 
    { 
     Account account = new Account(context.getString(R.string.account_name), context.getString(R.string.account_type)); 
     boolean accountCreated = AccountManager.get(context).addAccountExplicitly(account, context.getString(R.string.account_password), null); 
     if (accountCreated) 
     { 
      Log.d("TAG", "Account created"); 
     } 
     else 
     { 
      Log.d("TAG", "Account creation failed."); 
     } 
     return accountCreated; 
    } 

    public boolean doesMyAppAccountExist(Context context) 
    { 
     return AccountManager.get(context).getAccountsByType(context.getString(R.string.account_type)).length > 0; 
    } 
} 

を-name):

if (!new MyAppAccountManager().createMyAppAccountIfNotExists(context)) 
{ 
    throw new UnableToCreateContact("Could not find or create MyApp account"); 
} 

ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI); 
     builder.withValue(RawContacts.ACCOUNT_TYPE, context.getString(R.string.account_type)); 
     builder.withValue(RawContacts.ACCOUNT_NAME, context.getString(R.string.account_name)); 
     ops.add(builder.build()); 

さらに、私は私のマニフェストに自分のアカウント認証を記述する必要があります。

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

私のオーセンティケータサービスを指す:

public class MyAppAuthenticatorService 
     extends Service 
{ 
    @Override 
    public IBinder onBind(Intent intent) 
    { 
     MyAppAuthenticator authenticator = new MyAppAuthenticator(this); 
     return authenticator.getIBinder(); 
    } 
} 

デフォルトMyAppAuthenticatorが必要です。

public class MyAppAuthenticator 
     extends AbstractAccountAuthenticator 
{ 
    public MyAppAuthenticator(Context context) 
    { 
     super(context); 
    } 

    @Override 
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) 
    { 
     throw new UnsupportedOperationException(); 
    } 

    ... etc 

マニフェストはまた、オーセンティケータを指します。XML:最後に

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

- ちょうど絵が完全にするために - 文字列定数があります:

<string name="account_type">my.app.applicationid.account</string> 
<string name="account_name">@string/app_title</string> 
<string name="account_password">password</string> 
関連する問題