Android应用程序通过restful webservice

时间:2015-09-21 04:39:13

标签: android sql-server sqlite service timer

我创建了一个应用程序,每隔5分钟运行一次服务并从sqlite获取记录并将它们发送到服务器并更新sqlite中的状态,问题出现在sql server数据库中,其中插入了多行时间。

我不知道是什么导致了这个问题,在调试我的代码时我注意到当发送一行到服务器调试器进入一个类的Timer中,并再次发送相同的记录时,我有一种感觉,计时器一次又一次地运行,并在向服务器发送多条记录后更新状态。请说明问题是什么以及执行此任务的正确方法是什么。

这是代码服务类

public class AlarmServices extends Service {

private Timer myTimer;
private SQLiteDB dbHelper;
Cursor cursor;
String where = "1=1";
String Whatstatus;
String FFCode, TerrCode, FFMgr;

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            scheduleAlarm();
            getDataFromDB();

    }

    }, 0, 5 * 60 * 1000); // 5 min 
    Log.e("syn process start", "yes");
}


private void getDataFromDB() {

    dbHelper = new SQLiteDB(AlarmServices.this);
    dbHelper.open();
    cursor = dbHelper.getInCompleteAttendance(where);

    int rows = cursor.getCount();
    if (rows > 0) {
        cursor.moveToFirst();

        for (int i = 0; i < rows; i++) {
            int Id;
            String recvdate, ffcode, terr_code, drcode, drname, addr, createddate, ffmgr, shift, lat, lon,droidrefno,locAddress, sync_status, curr_date, date_only;
            Id = cursor.getInt(0);
            recvdate = cursor.getString(1);
            ffcode = cursor.getString(2);
            terr_code = cursor.getString(3);
            drcode = cursor.getString(4);
            drname = cursor.getString(5);
            addr = cursor.getString(6);
            createddate = cursor.getString(7);
            ffmgr = cursor.getString(8);
            shift = cursor.getString(9);
            lat = cursor.getString(10);
            lon = cursor.getString(11);
            droidrefno = cursor.getString(12);
            locAddress = cursor.getString(13);
            sync_status = cursor.getString(14);
            curr_date = cursor.getString(15);
            date_only = cursor.getString(16);

            // Sync Server
            ChkInsertDailyAttendanceOperation insertAttd = new ChkInsertDailyAttendanceOperation(AlarmServices.this,
                    recvdate, ffcode, terr_code, drcode, drname, locAddress,
                    createddate, ffmgr,shift,Double.parseDouble(lat),Double.parseDouble(lon),
                    droidrefno,curr_date,date_only);

             insertAttd.execute();
             cursor.moveToNext();

        }

    } else {

    }

    dbHelper.close();
}

public void scheduleAlarm() {

    Long time = new GregorianCalendar().getTimeInMillis() + 1000;
    Intent intentAlarm = new Intent(this, MyExtBroadcastReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent
            .getBroadcast(this, 1, intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT));

}

public static class MyExtBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Sync Process Running",
                  Toast.LENGTH_LONG).show();

        Log.e("sync process running","yes");
    }

}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
}

异步任务类的代码

public class ChkInsertDailyAttendanceOperation extends AsyncTask<String, Void, String> {

String serverResponse;
String Recv_Date, FF_code, Terr_code, Dr_code, Dr_name, Addr, Created_Date,Currdate,locAddr,
FF_mgr, Shift, Droid_Ref_No,DateOnly;
double Lat,Lng;
String Response ="{\"Value\":true,\"Successful\":true}"; //"{\"Successful\":true}";
Context cxt;
UserSQLiteDB dbHelper;

public ChkInsertDailyAttendanceOperation(Context context, String recv_Date,
        String ff_code, String terr_code, String dr_code, String dr_name,
        String locAddress,  String created_Date,String ff_mgr, String shift,double lat,double lng,String droid_Ref_No,String currdate,String dateonly  ) {
       // TODO Auto-generated constructor stub

    Recv_Date = recv_Date;
    FF_code = ff_code;
    Terr_code = terr_code;
    Dr_code = dr_code;
    Dr_name = dr_name;
    //Addr = addr;
    Created_Date = created_Date;
    FF_mgr = ff_mgr;
    Shift = shift;
    Lat = lat;
    Lng = lng;
    Droid_Ref_No =droid_Ref_No;
    locAddr = locAddress;
    Currdate =currdate;
    DateOnly =dateonly;
    cxt=context;
}

@Override
protected String doInBackground(String... urls) {
    RestAPI restAPI = new RestAPI();
    JSONObject jsonObj = new JSONObject();

    try {


        jsonObj = restAPI.UserDailyAttendance(Recv_Date, FF_code,
                Terr_code, Dr_code, Dr_name, locAddr, Created_Date, FF_mgr,
                Shift,Lat, Lng,Droid_Ref_No);
        serverResponse = jsonObj.toString();


    } catch (Exception e) {
        // TODO Auto-generated catch block
        serverResponse="InternetNotFound";
        Log.e("catch block", serverResponse);

    }

    Log.e("repsonse", serverResponse);
    return serverResponse;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    //pd.dismiss();
    dbHelper = new SQLiteDB(cxt);
    dbHelper.open();

        String SyncStatus;
        if (serverResponse.equals(Response)) {
            SyncStatus = "C";
        } else {
            SyncStatus = "I";
        }

        dbHelper.updateDAILYATTENDANCE(Recv_Date, FF_code,
                Terr_code, Dr_code, Dr_name, Addr, Created_Date, FF_mgr,
                Shift,String.valueOf(Lat), String.valueOf(Lng),Droid_Ref_No,locAddr,SyncStatus,Currdate,DateOnly);

        dbHelper.close(); 

}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //pd.show();
}

0 个答案:

没有答案