>
Olá! Por que só consigo executar a replicação apenas uma vez? Mesmo eu limpando da tela a notificação recebida, quando volto na aplicação e tento iniciar uma nova replicação não acontece nada, só consigo se reiniciar o celular... Nos logs não aparece nada quando clico no botão da replicação... é como se não tivesse nenhum código nele....
Outro problema.... depois que faço a replicação, mesmo que eu limpe a notificação, depois de um tempo aleatório.. sem mesmo eu mexer no celular ou tentar uma outra replicação, recebo uma nova notificação da replicação... sem que eu tenha feito outra... é a notificação da mesma replicação que já tinha recebido antes..
Segue o código abaixo:
package com.gestec.vendas;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
public class ExportarVendasService extends Service implements Runnable{
public void onCreate(){
Thread t = new Thread(ExportarVendasService.this);
t.start();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void run() {
SQLiteDatabase db = openOrCreateDatabase("vendas.db", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM vendas", null);
int totalDB = cursor.getCount();
int totalReplicado = 0;
while(cursor.moveToNext()){
StringBuilder strURL = new StringBuilder();
strURL.append("http://192.168.0.12/vendas/inserir.php?produto=");
strURL.append(cursor.getInt(cursor.getColumnIndex("produto")));
strURL.append("&preco=");
strURL.append(cursor.getDouble(cursor.getColumnIndex("preco")));
strURL.append("&latitude=");
strURL.append(cursor.getDouble(cursor.getColumnIndex("la")));
strURL.append("&longitude=");
strURL.append(cursor.getDouble(cursor.getColumnIndex("lo")));
Log.d("ExportarVendasService", strURL.toString());
try{
URL url = new URL(strURL.toString());
HttpURLConnection http = (HttpURLConnection) url.openConnection();
InputStreamReader ips = new InputStreamReader(http.getInputStream());
BufferedReader line = new BufferedReader((ips));
String linhaRetorno = line.readLine();
if(linhaRetorno.equals("Y")){
db.delete("vendas", "_id=?", new String[]{String.valueOf(cursor.getInt(0))});
totalReplicado++;
Log.d("ExportarVendasService", "OK");
}
} catch(Exception e){
Log.d("ExportarVendasService", e.getMessage());
}
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification nt = null;
if(totalDB == totalReplicado){
nt = new Notification(R.drawable.ic_launcher, "Status replicação", System.currentTimeMillis());
nt.flags |= Notification.FLAG_AUTO_CANCEL; // Limpa a notificação assim que ela for lida.
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this.getApplicationContext(), Main.class), 0);
nt.setLatestEventInfo(this, "Status replicação", "A replicação foi feita com sucesso, total: " + totalReplicado, p);
} else {
nt = new Notification(R.drawable.ic_launcher, "Status replicação", System.currentTimeMillis());
nt.flags |= Notification.FLAG_AUTO_CANCEL; //
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this.getApplicationContext(), Main.class), 0);
nt.setLatestEventInfo(this, "Status replicação", "Erro ao replicar, total: " + totalReplicado + " de " + totalDB, p);
}
nt.vibrate = new long[]{100, 2000, 1000, 2000};
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nt.sound = soundUri;
notificationManager.notify((int)Math.round(Math.random()), nt);
db.close();
}
}
Bom consegui resolver, não sei se é uma maneira correta de fazer isso, mas coloquei para parar o serviço depois de rodar a thread... está funcionando.....
stopService(new Intent("INICIAR_REPLICACAO"));
Pablo
Sim, ou use stopSelf();
Pois não é possível rodar ele se já está em execução, por isso apôs a replicação, você precisa "matar" ele.