我正在尝试将 SQLite 数据与 mysql 同步,为此创建了 JobService,它每 15 秒同步一次数据(仅同步那些状态与 mysql 不同步的数据)。这与 localhost(xampp 服务器)完美配合。但网络服务器出现问题:每 15 秒 JobService 会导致重复条目。
我的服务.java
public class MyService extends JobService {
private DatabaseHelper db;
SQLiteDatabase sqLiteDatabase;
private Handler mJobHandler = new Handler( new Handler.Callback() {
@Override
public boolean handleMessage( Message msg ) {
Toast.makeText( getApplicationContext(), "JobService task running", Toast.LENGTH_SHORT ).show();
jobFinished( (JobParameters) msg.obj, false );
return true;
}
} );
@Override
public boolean onStartJob(JobParameters params ) {
mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
db = DatabaseHelper.getInstance(getApplicationContext());
postData();
//db.close();
return true;
}
@Override
public boolean onStopJob( JobParameters params ) {
mJobHandler.removeMessages( 1 );
return false;
}
private void postData() {
Cursor cursor = db.getUnsyncedNames();
try {
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
saveName(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)), cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_FROM_NO)), cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_TO_NO)));
} while (cursor.moveToNext());
}
}
} finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
db.close();
}
}
private void saveName(final int id, final String fromNo, final String toNo) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_REQUEST_SMS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
if (!obj.getBoolean("error")) {
//updating the status in sqlite
db.updateNameStatus(id,NAME_SYNCED_WITH_SERVER);
sqLiteDatabase = db.getWritableDatabase();
if (sqLiteDatabase != null) {
sqLiteDatabase.delete(DatabaseHelper.TABLE_NAME, null, null);
}
// db.close();
Toast.makeText(getApplicationContext(),"Unsynced no. submitted!",Toast.LENGTH_LONG).show();
//calls.clear();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"JSONException error1 : "+e,Toast.LENGTH_LONG).show();
}
finally {
db.close();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"VolleyError error2 : "+error,Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("mobile", fromNo);
params.put("toMob", toNo);
return params;
}
};
int socketTimeout = 60000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
MyApplication.getInstance().addToRequestQueue(stringRequest);
}
}
MainActivity.java
JobInfo.Builder builder = new JobInfo.Builder( 1,
new ComponentName( getPackageName(), MyService.class.getName() ) );
builder.setPeriodic(15000);
我的logcat也显示
E/Surface:getSlotFromBufferLocked:未知缓冲区:0x7f517cd1b540