首页 > 解决方案 > 我可以在 Android 同步适配器中使用将由 onperform 同步方法修改的字段变量吗?

问题描述

我有一个 Android Sync 适配器,可以使用 volley 请求将本地数据推送到服务器。我正在使用整数变量 lastid 来检查最后同步的数据行。可以在同步适配器中使用可以通过 onPerformSync 方法修改的变量吗?如果同步适配器正在处理并行请求,它会影响同步吗?我在 syc 过程中随机丢失了一些数据?谁能帮我解决这个问题?

public class SyncAdapter extends AbstractThreadedSyncAdapter {

 // Define a variable to contain a content resolver instance
    ContentResolver mContentResolver;
    Context ctx;
    SQLiteDatabase syncdb=null;
    SQLiteDatabase mydbsalesdata=null;
    private String TAG="syncadapter";
    String lastid;

    /**
     * Set up the sync adapter
     */
    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        ctx=context;
        /*
         * 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
     */
    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();

    }

    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {

        /*
         * Put the data transfer code here.
         */

        if(extras!=null){
            if(extras.getString("table")!=null){
                String table=extras.getString("table");

                if(extras.getString("where")!=null){
                    String where =extras.getString("where");
                    if(extras.getString("operation").equalsIgnoreCase("update")){
                        syncUpdate(table,where);
                    }else if(extras.getString("operation").equalsIgnoreCase("delete")){
                        syncDelete(table,where);
                    }
                }else{
                    synctable(table);
                }

            }else if(extras.getString("download")!=null){
                download();
            }else{
                syncperiodic();
            }
        }else{
            syncperiodic();
        }

    }

标签: androidthread-safetybackground-processandroid-syncadapter

解决方案


推荐阅读