I have created an app where service runs after every 5 mins and get records from sqlite and send them to server and updates the status in the sqlite, the problem comes in the sql server database where a single row is inserted more than one time.
I don't know what thing is causing this problem, on debugging my code i noticed that when sending one row to server debugger goes in a build in class of Timer, and again send the same record, I have a feeling that the timer is running again and again and updates the status after sending multiple records to server. please suggest what is the problem and what is the correct way to perform this task.
this is the code service class
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");
}
}
code of async task class
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();
}
Aucun commentaire:
Enregistrer un commentaire