2013-07-23 2 views
1

캘린더 동기화 및 동기화 연락처 외에도 내 응용 프로그램 계정에서 내 노트 동기화 참고를 만들고 싶습니다. 이를 위해 내 자신의 사용자 지정 SyncAdapter를 만들었습니다. 그러나 여전히 내 계정에 이러한 옵션이 표시되지 않습니다.계정 관리자의 동기화 메뉴에 동기화 이름이 나열되지 않음

<provider 
      android:name="com.syncadapter.NotesContentProvider" 
      android:authorities="com.syncadapter" 
      android:label="Notes" 
      android:syncable="true" > 
     </provider> 
<service 
     android:name="com.mypack.auth.NoteSyncService" 
     android:exported="true" 
     android:process=":note" > 
     <intent-filter> 
      <action android:name="android.content.SyncAdapter" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.content.SyncAdapter" 
      android:resource="@xml/sync_note" /> 
    </service> 

및 XML에

매니페스트 파일

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" 
    android:accountType="com.mypack.auth" 
    android:contentAuthority="com.syncadapter" 
    android:supportsUploading="false" 
    android:userVisible="true" /> 

답변

1

문제는 여기에 를 해결 나는 다음과 같은 일을 만들 필요가 이것에 대한 내 요구 사항에 따라 나는 내 자신의 메모 동기화 메커니즘을 만들었습니다.

1) 콘텐츠 공급자를 확장하는 동기화 처리를위한 자체 공급자를 만듭니다. 2) 데이터 조작을위한 자체 서비스를 생성하십시오. 3) res/XML에 동기화 어댑터를 작성하십시오. 4) 매니페스트 파일에 제공자를 등록하십시오. 5) 서비스를 매니페스트 파일에 등록하십시오.

1) 콘텐츠 공급자를 확장하는 동기화 처리를위한 자체 공급자를 만듭니다.

public class Provider extends ContentProvider { 

private static final int CONSTANTS = 1; 
private static final int CONSTANT_ID = 2; 
private static final UriMatcher MATCHER; 
private static final String TABLE = "constants"; 
public static final class Constants implements BaseColumns { 

    public static final Uri CONTENT_URI = Uri 
    .parse("content://com.contentprovider.Provider/constants"); 
    public static final String DEFAULT_SORT_ORDER = "title"; 
    public static final String TITLE = "title"; 
    public static final String VALUE = "value"; 
} 

static { 
    Log.i("Provider", "Start"); 
    MATCHER = new UriMatcher(UriMatcher.NO_MATCH); 
    MATCHER.addURI("com.contentprovider.Provider", "constants", CONSTANTS); 
    MATCHER.addURI("com.contentprovider.Provider", "constants/#", 
    CONSTANT_ID); 
} 
DatabaseAdapter db = null; 

@Override 
public boolean onCreate() { 
    // db = new DatabaseHelper(getContext()); 
    Log.i("Provider", "Startw"); 
    db = new DatabaseAdapter(getContext()); 
    return ((db == null) ? false : true); 
} 

@Override 
public Cursor query(Uri url, String[] projection, String selection, 
    String[] selectionArgs, String sort) { 
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 

    qb.setTables(TABLE); 
    String orderBy; 

    if (TextUtils.isEmpty(sort)) { 
    orderBy = Constants.DEFAULT_SORT_ORDER; 
    } else { 
    orderBy = sort; 
    } 
    Cursor c = qb.query(db.getReadableDatabase(), projection, selection, 
    selectionArgs, null, null, orderBy); 
    c.setNotificationUri(getContext().getContentResolver(), url); 
    return (c); 
} 

@Override 
public String getType(Uri url) { 
    if (isCollectionUri(url)) { 
    return ("vnd.commonsware.cursor.dir/constant"); 
    } 
    return ("vnd.commonsware.cursor.item/constant"); 
} 

@Override 
public Uri insert(Uri url, ContentValues initialValues) { 
    long rowID = db.getWritableDatabase().insert(TABLE, Constants.TITLE, 
    initialValues); 

    if (rowID > 0) { 
    Uri uri = ContentUris.withAppendedId(
    Provider.Constants.CONTENT_URI, rowID); 
    getContext().getContentResolver().notifyChange(uri, null); 
    return (uri); 
    } 
    throw new SQLException("Failed to insert row into " + url); 
} 

@Override 
public int delete(Uri url, String where, String[] whereArgs) { 
    int count = db.getWritableDatabase().delete(TABLE, where, whereArgs); 
    getContext().getContentResolver().notifyChange(url, null); 
    return (count); 
} 

@Override 
public int update(Uri url, ContentValues values, String where, 
    String[] whereArgs) { 
    int count = db.getWritableDatabase().update(TABLE, values, where, 
    whereArgs); 
    getContext().getContentResolver().notifyChange(url, null); 
    return (count); 
} 

private boolean isCollectionUri(Uri url) { 
    return (MATCHER.match(url) == CONSTANTS); 
} 
} 

2) 데이터 조작을 위해 자체 서비스를 생성하십시오.

public class noteSyncService extends Service { 


     private static SyncAdapterImpl sSyncAdapter = null; 
     static int i = 0; 

     public noteSyncService() { 
     super(); 
     } 

     private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { 
    private Context mContext; 

    public SyncAdapterImpl(Context context) { 
    super(context, true); 
    mContext = context; 
    } 

     @Override 
    public void onPerformSync(Account account, Bundle extras, 
    String authority, ContentProviderClient provider, 
    SyncResult syncResult) { 
    account = null; 
    account = accountAccountManager.getAccount(
    mContext, 
    accountAccountManager.currentUser(mContext) 
     .get("username_display").toString()); 
    try { 
    if (account != null) { 
    noteSyncService.performSync(mContext, account, extras, 
     authority, provider, syncResult); 
    } 
    } catch (OperationCanceledException e) { 
    } 
    } 
    } 

    @Override 
public IBinder onBind(Intent intent) { 
    IBinder ret = null; 
    ret = getSyncAdapter().getSyncAdapterBinder(); 
    return ret; 
} 

private SyncAdapterImpl getSyncAdapter() { 
    if (sSyncAdapter == null) 
    sSyncAdapter = new SyncAdapterImpl(this); 
    return sSyncAdapter; 
} 

private static void performSync(Context context, Account account, 
    Bundle extras, String authority, ContentProviderClient provider, 
    SyncResult syncResult) throws OperationCanceledException { 

    try { 
       //Servive Start Handle Sync Process 
    } catch (Exception e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 
} 
} 

3) res/XML에 동기화 어댑터를 작성하십시오.

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" 

      android:accountType="com.account.auth" 
      android:contentAuthority="com.syncadapter" 
      android:label="Sync Notes" 
      android:supportsUploading="true" 
      android:userVisible="true" /> 

4) 매니페스트 파일에 제공자를 등록하십시오.

<provider 

      android:name="com.contentprovider.Provider" 
      android:authorities="com.syncadapter" 
      android:enabled="true" 
      android:exported="true" 
      android:label="Notes" 
      android:syncable="true" /> 

5) 서비스를 매니페스트 파일에 등록하십시오.

<service 

      android:name="com.account.auth.mySyncService" 
      android:exported="true" 
      android:label="Sync NOTES" > 
      <intent-filter> 
       <action android:name="android.content.SyncAdapter" /> 
      </intent-filter> 
      <meta-data 
       android:name="android.content.SyncAdapter" 
       android:resource="@xml/noteSyncService" /> 
</service>