Check if the integer value has increased?

I have the following code that checks the number of rows in a database.

private void checkMMSRows(){
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms/part"), null, null, null, null);
    if (curPdu.moveToNext()){
        int number = curPdu.getCount();
        System.out.println(number);
    }
}

I will run this code every second and do something when the value has changed. The problem is, how can I “detect” a change? Any help would be appreciated.

+5
source share
4 answers

Basically, add a class variable - you can either make it staticfor all instances of the class, or an instance variable (by deleting the keyword static).

, , oldNumber. oldNumber - - :

private static int oldNumber = -1;
private void checkMMSRows(){
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms/part"), null, null, null, null);
    if (curPdu.moveToNext()){
        int number = curPdu.getCount();
        System.out.println(number);
        if(number != oldNumber){
            System.out.println("Changed");
            // add any code here that you want to react to the change
        }
        oldNumber = number;
    }
}

Update:

, , , amit answer.

+4

, . , , , , , , int currentValue, , , ( )

int currentValue = 0;
private void checkMMSRows(){
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms/part"), null, null, null, null);
    if (curPdu.moveToNext()){
        int newValue = curPdu.getCount();
        if (newvalue != currentValue) {
               //detected a change
               currentValue = newValue;
        }
        System.out.println(newValue);
    }
}
+4

, , , : - .

.

, , , .

EDIT:
java Observer Observable, .

+3

braodcastreceiver , , .

0

All Articles