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" />
4

1 回答 1

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>
于 2013-08-07T07:25:05.523 回答