Problem in my notification system

2

The issue is that I'm having problems with my notifications system because it reads an xml, and saves the id with sharedPreferences, so when it comes to verifying the xml again it compares the saved id with the id coming from the current whole xml The problem is that sometimes it is notifying me even if I do not update anything in the xml, see how the code is:

public class NotificationService extends Service {

SharedPreferences settings;
String idSalvo;
ItemXml item;
public static final String PREFS_NAME = "PrefXml";

public void onCreate() {

    super.onCreate();
     item = new ItemXml();

    settings = getSharedPreferences(PREFS_NAME, 0);

}

@Override     public int onStartCommand (Intent intent, int flags, int startId) {

    verificaNotificacao();
    return START_STICKY;

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

public void VerificationNotification () {         if (checkConnection ()) {// if you have a connection             getXml (); // will load the xml saving the current id             readValue (); // will read the value you have saved in the current device to compare

        if (!idSalvo.equals(item.getId())) { //compara os dois
            idSalvo = item.getId();// se for diferente ele iguala
            gerarNotificacao(); // e notifica
            saveValue(); //depois salva o novo fa
            // salva o valor atual
        }
    }
    stopSelf(); // mata o serviço
}


/*
 * Metodo que vai carregar os items do xml atual
 */

public void getXml() {
    try {
        // Cria o leitor de RSS
        XmlReader rssReader = new XmlReader(
                "http://xxx.xxx.xxx/teste.xml");
        item.setId(rssReader.getItems().get(0).getId()); // item recebe o id do xml atual

    } catch (Exception e) {
        Log.e("notificações", e.getMessage());
    }
}

/*
 * Salvando valor do id com sharedPreferences
 */
public void saveValue() {
    try {
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("idXml", idSalvo);

        // Confirma a gravação dos dados
        editor.commit();
    } catch (Exception e) {
        Log.e("Notificações", e.getMessage());
    }
}

// Vai ler o valor salvo no dispositivo
public void readValue() {

    idSalvo = settings.getString("idXml", "");
}

}
    
asked by anonymous 17.10.2014 / 22:39

2 answers

1

I found the solution, it was because the value of the xml was coming null when the server crashed, so I forgot to handle the null value and it notified, but now I've dealt with it.

    
20.10.2014 / 22:21
0

The Android documentation indicates, on SharedPreferences "This data will persist across user sessions (even if your application is killed)". Translation: "This data persists between sessions, even though your application is dead."

So there must be another problem.

URL: link

    
17.10.2014 / 23:41