1

我对 GcmTaskService 有疑问。当我在创建 GcmTaskService 之前尝试在活动中更新数据库时,似乎数据库没有更新,并且以前的值仍然存在。这是我的 GcmTaskService

public class GcmSendInfoService extends GcmTaskService {
    @Override
    public int onRunTask(TaskParams taskParams) {


        sendDataToserver(getApplicationContext());
        Log.i("update Task", "service run for update");
        return GcmNetworkManager.RESULT_SUCCESS;
    }
@Override
public void onCreate() {
    Log.i("onCreate sendToken Task", "send Token");
    super.onCreate();
    CreateSendInfoModel.createGcmNetwork(getApplicationContext());
 }
}

我在活动中调用此方法

  private void resetPushFlags(Context context) {
        Cursor userCursor = DataBase.TableCompanyUser.
                getSomeObjsInTable(context);

        while (!userCursor.isAfterLast()) {

            SomeClass someObj= new SomeClass(userCursor);
            someObj.setFlgFirst(false);
            DataBase.TableCompanyUser.update(context, someObj);
            userCursor.moveToNext();
        }
    }

数据库更新方法在这里

      public static boolean update(Context context, SomeClass someObj) {

            ContentValues contentValues = new ContentValues();

            //id
            contentValues.put(some._ID, some.getId());
            //2

            if (some.isFlgFirst()) {
                contentValues.put(someObj.FLG_FIRST, 1);

            } else {
                contentValues.put(someObj.FLG_FIRST, 0);

            }

            String whereClause = Some._ID + " =? ";
            String[] whereArgs = new String[] 
            {String.valueOf(someObj.getId())};


            int numberOfRow = context.getContentResolver()
                    .update(SomeTable.CONTENT_URI, contentValues, whereClause, whereArgs);

            return numberOfRow > 0;
        }

我创建 GcmServiceTask 如下

static long ONE_MINUTE_SECONDS = 60L;
    static String PERIODIC_TAG = "PERIODIC_SEND_IS_TAG";

    public static void createGcmNetwork(Context context) {
        GcmNetworkManager mGcmNetworkManager = GcmNetworkManager.getInstance(context);
        PeriodicTask task = new PeriodicTask.Builder()
                .setService(GcmSendInfoService.class)
                .setTag(PERIODIC_TAG)
                .setPeriod(ONE_MINUTE_SECONDS)
                .setPersisted(true)
                .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
                .setUpdateCurrent(true)
                .setRequiresCharging(false)
                .build();

        mGcmNetworkManager.schedule(task);

    }

那么如何使 GcmTaskService 与数据库更改同步呢?

4

0 回答 0