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", "");
}
}
I’ve changed the subject of the question, if you can help me I’ll be grateful.
– War Lock