【问题标题】:Sync Name Not Listed in Sync Menu in Account Manger同步名称未在帐户管理器的同步菜单中列出
【发布时间】:2013-07-22 02:16:42
【问题描述】:

我想在我的应用程序帐户下创建自己的选项同步笔记,除了同步日历和同步联系人。为此,我创建了自己的自定义 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" />

【问题讨论】:

    标签: android performance android-intent android-emulator


    【解决方案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)为数据操作创建自己的Service。

    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。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多