android - 帐户管理器的同步菜单中未列出同步名称

标签 android performance android-intent android-emulator

除了同步日历和同步联系人之外,我想在我的应用程序帐户下创建自己的选项同步笔记。为此,我创建了自己的自定义 SyncAdapter。但是,我仍然无法在我的帐户下看到这些选项。

list 文件

<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)创建自己的提供程序来处理扩展的同步 内容提供商。 2)创建自己的数据操作服务。 3) 在 res/XML 中创建同步适配器。 4)在您的 list 文件中注册提供者。 5)在 list 文件中注册您的服务。

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)在您的 list 文件中注册提供者。

<provider

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

5)在 list 文件中注册您的服务。

<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>

关于android - 帐户管理器的同步菜单中未列出同步名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17802940/

相关文章:

android - 在不使用 HttpEntity 的情况下使用 Volley 发布 Multipart 并取得进展

java - 无法从sqlite数据库android中删除行

android - 从第三个 Activity 设置主 Activity 中的列表数据?

java - ListView 未正确填充/不可见

performance - Membase 和 Redis 必须存储在磁盘上时

java - Android - 在本地存储 'large' 数据

java - SearchView 不启动 SearchableActivity

android - 在启动器中没有图标的市场中提供 "open"

Android 深层链接到服务?

performance - Asp.net Mvc 2 DisplayFor 性能问题?