我已经展示了在ACCOUNT MANAGER下获取自己的菜单以提供同步机制的方法
答案 0 :(得分:0)
解决问题在这里,我根据自己的要求创建了自己的同步笔记机制,我必须创建以下内容。
1)创建自己的提供程序以处理扩展Content Provider的同步。 2)为数据操作创建自己的服务。 3)在res / XML中创建同步适配器。 4)在清单文件中注册提供程序。 5)在清单文件中注册您的服务。
1)创建自己的提供程序来处理扩展Content Provider的同步。
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>