Getting 2 SQLite records for every email received

Go To StackoverFlow.com

0

I'm very new to android, and programming in general. I've got an email receiver using the K9 email client. I'm checking the email for proper formatting and taking the contents of the email and entering it into an SQLite database on the phone. The receiver is working properly. The problem is that when I send a properly formatted message, it makes 2 records from the first email. If I send another message without deleting the first one, it makes 4 records of the second. The third makes 6 records, etc. I know there is probably something obvious that I'm missing but I can't find the problem.

Here is the code:

    public class EmailReceiver extends BroadcastReceiver{
private JobsData jobs;
public static final Uri k9uri = Uri.parse("content://com.fsck.k9.messageprovider/inbox_messages/");
static String[] messages_projection = new String[] {"subject", "preview", "unread"};


@Override
public void onReceive(Context context, Intent intent) {
    try {
        Context mContext = context;
        Cursor curSt = mContext.getContentResolver().query(k9uri, messages_projection, "unread='true'", null, null);
        curSt.moveToFirst();
        String preview = null;
        String subject = null;
        jobs = new JobsData(context);
        int i = 0;
        while (!curSt.isAfterLast()) {
            subject = curSt.getString(0);
            boolean test = subject.startsWith("JOB# ");
            if (test) {
                boolean check = true;
                try {
                    for (int k = 5; k < subject.length(); k++) {
                        if (!Character.isDigit(subject.charAt(k))) {
                            check = false;
                            break;
                        }
                    }
                } catch (Exception e) {
                } finally {
                }

                if (check) {
                    preview = curSt.getString(1);
                    compareDb(subject.substring(7), preview, context);
                }

            }
            curSt.moveToNext();
        }
        curSt.close();
    } catch (Exception e) {
        Toast toast = Toast.makeText(context, e.toString(), Toast.LENGTH_LONG);
        toast.show();
    }

    if (emails != null) {

     }
}


private void compareDb (String jobNo, String preview, Context context) {
    try {
    String[] dbJobs = new String[] {"JobNo"};
    SQLiteDatabase db = jobs.getReadableDatabase();
    Cursor dbCur = db.query(tableName, dbJobs, null, null, null, null, null);
    dbCur.moveToFirst();
    while (!dbCur.isAfterLast()) {
        if (!jobNo.equals(dbCur.getString(0))) {
            jobExtractor(preview, jobNo, context);
        }
        dbCur.moveToNext();
    }
    dbCur.close();
    } catch (Exception e){
        Toast toast = Toast.makeText(context, e.toString(), Toast.LENGTH_LONG);
        toast.show();

    }
}

private void addJobs(Job job){
    SQLiteDatabase db = jobs.getWritableDatabase();
    ContentValues values = new ContentValues();
        values.put(custName, job.getCustName());
        values.put(jobNumber, job.getJobNumber());
        values.put(jobType, job.getJobType());
        values.put(address, job.getAddress());
        values.put(zip, job.getZip());
        values.put(contact, job.getContact());
        values.put(contactNumber, job.getContactNumber());
        values.put(problem, job.getProblem());

    db.insertOrThrow(tableName, null, values);
    db.close();

}

The "jobextractor" method works fine so I didn't include it. If someone can please help me I would appreciate it.

Thanks

Just a side note. I also want it to only look at unread email but that's not working either. Not a major problem at this point but if you have an answer to that I would be very grateful.

2012-04-03 22:23
by androidnoob


0

I foundthe problem.

while (!dbCur.isAfterLast()) { 
    if (!jobNo.equals(dbCur.getString(0))) { 
        jobExtractor(preview, jobNo, context); 
    } 
    dbCur.moveToNext(); 
}

The problem was here. I was checking to see if the job number matched existing records in the database. It went through each record in the table and added a new job for every record that didn't match. I replaced the jobExctractor line with a boolean check and if the check returned true after the loop I didn't add the job.

2012-04-26 15:55
by androidnoob