当网络可用且内容提供商数据发生变化时,如何在Android syncadapter上进行onPerformSync?

时间:2014-05-26 14:32:24

标签: java android android-contentprovider android-syncadapter

在同步适配器上实现Android开发人员教程时,http://developer.android.com/training/sync-adapters/creating-sync-adapter.html我最终在我的活动中使用了以下代码:

Activity.java

// Constants
    // The authority for the sync adapter's content provider
    public static final String AUTHORITY = "sync.adapter.datacontentprovider";
    // An account type, in the form of a domain name
    public static final String ACCOUNT_TYPE = "com.udinic.sync_example";
    // The account name
    public static final String ACCOUNT = "Udinic";
    // Instance fields
    Account mAccount;
    // Global variables
    // A content resolver for accessing the provider
    ContentResolver mResolver;

在onCreate(){

    mAccount = CreateSyncAccount(this);
// Get the content resolver for your app
mResolver = getContentResolver();
// Turn on automatic syncing for the default account and authority
mResolver.setSyncAutomatically(mAccount, AUTHORITY, true);

方法CreateSyncAccount:

public static Account CreateSyncAccount(Context context) {
            // Create the account type and default account
            Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
            // Get an instance of the Android account manager
            AccountManager accountManager = (AccountManager) context
                    .getSystemService(ACCOUNT_SERVICE);
            /*
             * Add the account and account type, no password or user data If
             * successful, return the Account object, otherwise report an error.
             */
            if (accountManager.addAccountExplicitly(newAccount, null, null)) {
                /*
                 * If you don't set android:syncable="true" in in your <provider>
                 * element in the manifest, then call context.setIsSyncable(account,
                 * AUTHORITY, 1) here.
                 */
            } else {
                /*
                 * The account exists or some other error occurred. Log this, report
                 * it, or handle it internally.
                 */
                Log.d("insitu", "creating sync account error");
            }
            return newAccount;
        }

这是Android开发人员教程中用于在可用连接启动时运行同步适配器的同步适配器的标准方法。

虽然我想要这个,但我也希望它只在我实施的ContentProvider中有新数据时才会发生。

public class datacontentprovider extends ContentProvider {

    private static final String TAG = "DataContentProvider";
    private static final String DATABASE_NAME = "data.db";
    private static final int DATABASE_VERSION = 1;

    // table names
    private static final String LOCATION_TABLE_NAME = "location";
    private static final String PICTURE_TABLE_NAME = "picture";
    private static final String ACCELEROMETER_TABLE_NAME = "accelerometer";
    private static final String SOUND_TABLE_NAME = "sound";
    private static final String BATTERY_TABLE_NAME = "battery";
    private static final String ORIENTATION_TABLE_NAME = "orientation";
    private static final String LIGHT_TABLE_NAME = "light";

    public static final String AUTHORITY = "sync.adapter.datacontentprovider";

    public static final Uri CONTENT_URI_LOCATION = Uri.parse("content://"
            + AUTHORITY + "/location");
    public static final Uri CONTENT_URI_PICTURE = Uri.parse("content://"
            + AUTHORITY + "/picture");
    public static final Uri CONTENT_URI_ACCELEROMETER = Uri.parse("content://"
            + AUTHORITY + "/accelerometer");
    public static final Uri CONTENT_URI_SOUND = Uri.parse("content://"
            + AUTHORITY + "/sound");
    public static final Uri CONTENT_URI_BATTERY = Uri.parse("content://"
            + AUTHORITY + "/battery");
    public static final Uri CONTENT_URI_ORIENTATION = Uri.parse("content://"
            + AUTHORITY + "/orientation");
    public static final Uri CONTENT_URI_LIGHT = Uri.parse("content://"
            + AUTHORITY + "/light");

    /*public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.jwei512.notes";
    public static final String NOTE_ID = "_id";
    public static final String TITLE = "title";
    public static final String TEXT = "text";*/

    private static final UriMatcher sUriMatcher;

    private static final int LOCATION = 1;
    private static final int LOCATION_ID = 2;
    private static final int PICTURE = 3;
    private static final int PICTURE_ID = 4;
    private static final int ACCELEROMETER = 5;
    private static final int ACCELEROMETER_ID = 6;
    private static final int SOUND = 7;
    private static final int SOUND_ID = 8;
    private static final int BATTERY = 9;
    private static final int BATTERY_ID = 10;
    private static final int ORIENTATION = 11;
    private static final int ORIENTATION_ID = 12;
    private static final int LIGHT = 13;
    private static final int LIGHT_ID = 14;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, LOCATION_TABLE_NAME, LOCATION);
        sUriMatcher.addURI(AUTHORITY, LOCATION_TABLE_NAME + "/#", LOCATION_ID);
        sUriMatcher.addURI(AUTHORITY, PICTURE_TABLE_NAME, PICTURE);
        sUriMatcher.addURI(AUTHORITY, PICTURE_TABLE_NAME + "/#", PICTURE_ID);
        sUriMatcher.addURI(AUTHORITY, ACCELEROMETER_TABLE_NAME, ACCELEROMETER);
        sUriMatcher.addURI(AUTHORITY, ACCELEROMETER_TABLE_NAME + "/#",
                ACCELEROMETER_ID);
        sUriMatcher.addURI(AUTHORITY, SOUND_TABLE_NAME, SOUND);
        sUriMatcher.addURI(AUTHORITY, SOUND_TABLE_NAME + "/#", SOUND_ID);
        sUriMatcher.addURI(AUTHORITY, BATTERY_TABLE_NAME, BATTERY);
        sUriMatcher.addURI(AUTHORITY, BATTERY_TABLE_NAME + "/#", BATTERY_ID);
        sUriMatcher.addURI(AUTHORITY, ORIENTATION_TABLE_NAME, ORIENTATION);
        sUriMatcher.addURI(AUTHORITY, ORIENTATION_TABLE_NAME + "/#",
                ORIENTATION_ID);
        sUriMatcher.addURI(AUTHORITY, LIGHT_TABLE_NAME, LIGHT);
        sUriMatcher.addURI(AUTHORITY, LIGHT_TABLE_NAME + "/#", LIGHT_ID);

    }

    private DatabaseHelper dbHelper;

    public class DatabaseHelper extends SQLiteOpenHelper {

        /**
         * table for locations | _id | _latitude | _longitude | _time |
         * _provider
         */
        public static final String TABLE_LOCATION = "location";
        public static final String COLUMN_LOCID = "_id";
        public static final String COLUMN_LATITUDE = "_latitude";
        public static final String COLUMN_LONGITUDE = "_longitude";
        public static final String COLUMN_TIME = "_time";
        public static final String COLUMN_ACCURACY = "_accuracy";
        public static final String COLUMN_PROVIDER = "_provider";

        /**
         * table for pictures | _id | _url | _latitude | _longitude | _time
         */
        public static final String TABLE_PICTURE = "picture";
        public static final String COLUMN_PIC_ID = "_id";
        public static final String COLUMN_PIC_URL = "_url";
        public static final String COLUMN_PIC_LATITUDE = "_latitude";
        public static final String COLUMN_PIC_LONGITUDE = "_longitude";
        public static final String COLUMN_PIC_TIME = "_time";
        public static final String COLUMN_PIC_ACCURACY = "_accuracy";

        /**
         * table for Accelerometer | _id | _x | _y | _z | _time
         */
        public static final String TABLE_ACCELEROMETER = "accelerometer";
        public static final String COLUMN_ACCELEROMETER_ID = "_id";
        public static final String COLUMN_ACCELEROMETER_X = "_x";
        public static final String COLUMN_ACCELEROMETER_Y = "_y";
        public static final String COLUMN_ACCELEROMETER_Z = "_z";
        public static final String COLUMN_ACCELEROMETER_TIME = "_time";

        /**
         * table for Sound | _id | _amplitude | _time
         */
        public static final String TABLE_AMPLITUDE = "sound";
        public static final String COLUMN_AMPLITUDE_ID = "_id";
        public static final String COLUMN_AMPLITUDE_AMPLITUDE = "_amplitude";
        public static final String COLUMN_AMPLITUDE_TIME = "_time";

        /**
         * table for Battery | _id | _status | _ischarging | _chargeplug |
         * _usbcharge | _accharge | _level | _scale | _batterypct | _time
         */
        public static final String TABLE_BATTERY = "battery";
        public static final String COLUMN_BATTERY_ID = "_id";
        public static final String COLUMN_BATTERY_STATUS = "_status";
        public static final String COLUMN_BATTERY_ISCHARGING = "_ischarging";
        public static final String COLUMN_BATTERY_CHARGEPLUG = "_chargeplug";
        public static final String COLUMN_BATTERY_USBCHARGE = "_usbcharge";
        public static final String COLUMN_BATTERY_ACCHARGE = "_accharge";
        public static final String COLUMN_BATTERY_LEVEL = "_level";
        public static final String COLUMN_BATTERY_SCALE = "_scale";
        public static final String COLUMN_BATTERY_BATTERYPCT = "_batterypct";
        public static final String COLUMN_BATTERY_TIME = "_time";

        /**
         * table for Orientation | _id | _azimut | _time
         */
        public static final String TABLE_ORIENTATION = "orientation";
        public static final String COLUMN_ORIENTATION_ID = "_id";
        public static final String COLUMN_ORIENTATION_AZIMUT = "_azimut";
        public static final String COLUMN_ORIENTATION_TIME = "_time";

        /**
         * table for Light | _id | _luminosity | _time
         */
        public static final String TABLE_LIGHT = "light";
        public static final String COLUMN_LIGHT_ID = "_id";
        public static final String COLUMN_LIGHT_LUMINOSITY = "_luminosity";
        public static final String COLUMN_LIGHT_TIME = "_time";

        private static final String DATABASE_NAME = "memory.db";
        private static final int DATABASE_VERSION = 1;

        // Database creation sql statement for location
        private static final String DATABASE_CREATE_LOC = "create table "
                + TABLE_LOCATION + "(" + COLUMN_LOCID
                + " integer primary key autoincrement, " + COLUMN_LATITUDE
                + " double not null, " + COLUMN_LONGITUDE
                + " double not null, " + COLUMN_TIME + " text not null, "
                + COLUMN_ACCURACY + " text not null, " + COLUMN_PROVIDER
                + " text not null " + ");";

        // Database creation sql statement for picture
        private static final String DATABASE_CREATE_PIC = "create table "
                + TABLE_PICTURE + "(" + COLUMN_PIC_ID
                + " integer primary key autoincrement, " + COLUMN_PIC_URL
                + " text not null, " + COLUMN_PIC_LATITUDE + " double, "
                + COLUMN_PIC_LONGITUDE + " double, " + COLUMN_PIC_TIME
                + " text, " + COLUMN_PIC_ACCURACY + " text not null" + ");";

        // Database creation sql statement for accelerometer
        private static final String DATABASE_CREATE_ACCELEROMETER = "create table "
                + TABLE_ACCELEROMETER
                + "("
                + COLUMN_ACCELEROMETER_ID
                + " integer primary key autoincrement, "
                + COLUMN_ACCELEROMETER_X
                + " float not null, "
                + COLUMN_ACCELEROMETER_Y
                + " float not null, "
                + COLUMN_ACCELEROMETER_Z
                + " float not null, "
                + COLUMN_ACCELEROMETER_TIME + " text not null" + ");";

        // Database creation sql statement for amplitude
        private static final String DATABASE_CREATE_AMPLITUDE = "create table "
                + TABLE_AMPLITUDE + "(" + COLUMN_AMPLITUDE_ID
                + " integer primary key autoincrement, "
                + COLUMN_AMPLITUDE_AMPLITUDE + " double not null, "
                + COLUMN_AMPLITUDE_TIME + " text not null" + ");";

        // Database creation sql statement for battery
        private static final String DATABASE_CREATE_BATTERY = "create table "
                + TABLE_BATTERY + "(" + COLUMN_BATTERY_ID
                + " integer primary key autoincrement, "
                + COLUMN_BATTERY_STATUS + " integer not null, "
                + COLUMN_BATTERY_ISCHARGING + " integer not null, "
                + COLUMN_BATTERY_CHARGEPLUG + " integet not null, "
                + COLUMN_BATTERY_USBCHARGE + " integer not null, "
                + COLUMN_BATTERY_ACCHARGE + " integer not null, "
                + COLUMN_BATTERY_LEVEL + " integer not null, "
                + COLUMN_BATTERY_SCALE + " integer not null, "
                + COLUMN_BATTERY_BATTERYPCT + " float not null,  "
                + COLUMN_BATTERY_TIME + " text not null" + ");";

        // Database creation sql statement for orientation
        private static final String DATABASE_CREATE_ORIENTATION = "create table "
                + TABLE_ORIENTATION
                + "("
                + COLUMN_ORIENTATION_ID
                + " integer primary key autoincrement, "
                + COLUMN_ORIENTATION_AZIMUT
                + " float not null, "
                + COLUMN_BATTERY_TIME + " text not null" + ");";

        // Database creation sql statement for light
        private static final String DATABASE_CREATE_LIGHT = "create table "
                + TABLE_LIGHT + "(" + COLUMN_LIGHT_ID
                + " integer primary key autoincrement, "
                + COLUMN_LIGHT_LUMINOSITY + " float not null, "
                + COLUMN_LIGHT_TIME + " text not null" + ");";

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL(DATABASE_CREATE_LOC);
            database.execSQL(DATABASE_CREATE_PIC);
            database.execSQL(DATABASE_CREATE_ACCELEROMETER);
            database.execSQL(DATABASE_CREATE_AMPLITUDE);
            database.execSQL(DATABASE_CREATE_BATTERY);
            database.execSQL(DATABASE_CREATE_ORIENTATION);
            database.execSQL(DATABASE_CREATE_LIGHT);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(MySQLiteHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATION);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PICTURE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCELEROMETER);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_AMPLITUDE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_BATTERY);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORIENTATION);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIGHT);
            onCreate(db);
        }

    }

    @Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext());
        return true;
    }

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

        switch (sUriMatcher.match(uri)) {
        case LOCATION:
            qb.setTables(LOCATION_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case LOCATION_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case PICTURE:
            qb.setTables(PICTURE_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case PICTURE_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case ACCELEROMETER:
            qb.setTables(ACCELEROMETER_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case ACCELEROMETER_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case SOUND:
            qb.setTables(SOUND_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case SOUND_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case BATTERY:
            qb.setTables(BATTERY_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case BATTERY_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case ORIENTATION:
            qb.setTables(ORIENTATION_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case ORIENTATION_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        case LIGHT:
            qb.setTables(LIGHT_TABLE_NAME);
            //qb.setProjectionMap(dataProjectionMap);
            break;
        case LIGHT_ID:
            selection = selection + "_id = " + uri.getLastPathSegment();
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null,
                null, sortOrder);

        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        /*switch (sUriMatcher.match(uri)) {
        case LOCATION:
            return Notes.CONTENT_TYPE;
        case PICTURE:
            return Notes.CONTENT_TYPE;
        case ACCELEROMETER:
            return Notes.CONTENT_TYPE;
        case SOUND:
            return Notes.CONTENT_TYPE;
        case BATTERY:
            return Notes.CONTENT_TYPE;
        case ORIENTATION:
            return Notes.CONTENT_TYPE;
        case LIGHT:
            return Notes.CONTENT_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }*/
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        if (sUriMatcher.match(uri) != LOCATION
                || sUriMatcher.match(uri) != PICTURE
                || sUriMatcher.match(uri) != ACCELEROMETER
                || sUriMatcher.match(uri) != SOUND
                || sUriMatcher.match(uri) != BATTERY
                || sUriMatcher.match(uri) != ORIENTATION
                || sUriMatcher.match(uri) != LIGHT) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long rowId = 0;
        Uri _uri = null;

        switch (sUriMatcher.match(uri)) {
        case LOCATION:
            rowId = db.insert(LOCATION_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_LOCATION, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case PICTURE:
            rowId = db.insert(PICTURE_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_PICTURE, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case ACCELEROMETER:
            rowId = db.insert(ACCELEROMETER_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_ACCELEROMETER, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case SOUND:
            rowId = db.insert(SOUND_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_SOUND, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case BATTERY:
            rowId = db.insert(BATTERY_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_BATTERY, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case ORIENTATION:
            rowId = db.insert(ORIENTATION_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_ORIENTATION, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        case LIGHT:
            rowId = db.insert(LIGHT_TABLE_NAME, null/*Notes.TEXT*/, values);
            if (rowId > 0) {
                _uri = ContentUris.withAppendedId(CONTENT_URI_LIGHT, rowId);
                getContext().getContentResolver().notifyChange(_uri, null);

            }
            break;
        default:
            throw new SQLException("Failed to insert row into " + uri);
        }
        return _uri;
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count = 0;

        switch (sUriMatcher.match(uri)) {
        case LOCATION:
            count = db.delete(LOCATION_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case LOCATION_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case PICTURE:
            count = db.delete(PICTURE_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case PICTURE_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case ACCELEROMETER:
            count = db.delete(ACCELEROMETER_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case ACCELEROMETER_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case SOUND:
            count = db.delete(SOUND_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case SOUND_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case BATTERY:
            count = db.delete(BATTERY_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case BATTERY_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case ORIENTATION:
            count = db.delete(ORIENTATION_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case ORIENTATION_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;
        case LIGHT:
            count = db.delete(LIGHT_TABLE_NAME, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case LIGHT_ID:
            where = where + "_id = " + uri.getLastPathSegment();
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }


        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where,
            String[] whereArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case LOCATION:
            count = db.update(LOCATION_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case PICTURE:
            count = db.update(PICTURE_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case ACCELEROMETER:
            count = db.update(ACCELEROMETER_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case SOUND:
            count = db.update(SOUND_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case BATTERY:
            count = db.update(BATTERY_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case ORIENTATION:
            count = db.update(ORIENTATION_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        case LIGHT:
            count = db.update(LIGHT_TABLE_NAME, values, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        return count;
    }

}

我认为这将在onPerformSync

syncadapter.java方法中完成
public class SyncAdapter extends AbstractThreadedSyncAdapter {
    // ...
    // Global variables
    // Define a variable to contain a content resolver instance
    ContentResolver mContentResolver;

    /**
     * Set up the sync adapter
     */
    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        /*
         * If your app uses a content resolver, get an instance of it from the
         * incoming Context
         */
        mContentResolver = context.getContentResolver();
    }

    // ...
    /**
     * Set up the sync adapter. This form of the constructor maintains
     * compatibility with Android 3.0 and later platform versions
     */
    @SuppressLint("NewApi")
    public SyncAdapter(Context context, boolean autoInitialize,
            boolean allowParallelSyncs) {
        super(context, autoInitialize, allowParallelSyncs);
        /*
         * If your app uses a content resolver, get an instance of it from the
         * incoming Context
         */
        mContentResolver = context.getContentResolver();
        // ...
    }

    /*
     * Specify the code you want to run in the sync adapter. The entire sync
     * adapter runs in a background thread, so you don't have to set up your own
     * background processing.
     */
    @Override
    public void onPerformSync(Account account, Bundle extras, String authority,
            ContentProviderClient provider, SyncResult syncResult) {
        /*
         * Put the data transfer code here.
         */
        // ...

    }
}

但是我怎么能contentProvider里面的onPerformSync知道它在contentprovider方法中知道它改变了并发送了改变的数据?我已经可以将存储在{{1}}内的数据类型发送到Web服务器。我只需就可以了。

1 个答案:

答案 0 :(得分:1)

这真的不是什么大不了,我明白我只需要执行

contentProvider.onRequestSync和Sync框架中的SyncService将检测到它并在SyncAdapter中调用onPerformSync