Problem in my notification system

Asked

Viewed 178 times

2

The point is that I am having problems in my notification system, because it reads an xml, and saves the id with sharedPreferences, so when checking the xml again it compares the saved id with the id coming from the current xml every time it checks, if it is different notifies, the problem is that sometimes you are notifying even without me updating anything in xml, see how the code is:

public class Notificacaoservice 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 checkNotificacao() { if (checkout()) { //if connection getXml(); // will load the xml saving the current id readValue(); // will read the value you have saved in the current feature 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", "");
}

}

2 answers

1


I found the solution, it was because the xml value la was coming null when the server fell, so I forgot to treat the null value and it notified, but now I treated it.

0

The Android document indicates, about Sharedpreferences "This data will persist Across user Sessions (Even if your application is Killed)". Translation: "This data persists between sessions, even if your application is killed."

Then there must be another problem.

URL: http://developer.android.com/guide/topics/data/data-storage.html

  • I’ve changed the subject of the question, if you can help me I’ll be grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.