>
Bom dia, eu fiz o projeto do curso com a Classe Services, e estou com um problema na hora da replicação. Quando eu clico no botão replicar após compilar a app ele replica mas se eu clicar depois, ou seja, uma segunda vez sem recompilar ele demora, demora muito, e não replica. ai quando eu pressiono o botão desktop do android para vê o que esta sendo executado e limpo ai ele replica. O que pode está acontecendo?
package br.com.example.localiza;
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.os.IBinder;
import android.util.Log;
public class ExportarVendasServiceActivity extends Service implements Runnable{
public void onCreate(){
new Thread(ExportarVendasServiceActivity.this).start();
}
@Override
public IBinder onBind(Intent arg0) {
// 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.getColumnCount(); //Variavel pega o total de registros no db
int TotalReplicado = 0; //Variavel pega o total de replicados
while(cursor.moveToNext()){
//http://7master.com.br/Insert.php?id_venda=1&produto=1&preco=2.5&latitude=13131&longitude=45454
StringBuilder strURL = new StringBuilder();
strURL.append("http://7master.com.br/Insert.php?id_venda=");
strURL.append(cursor.getInt(cursor.getColumnIndex("_id")));
strURL.append("&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("latitude")));
strURL.append("&longitude=");
strURL.append(cursor.getDouble(cursor.getColumnIndex("longitude")));
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.execSQL("delete from vendas where _id ="+new String[]{String.valueOf(cursor.getInt(0))}, null);
TotalReplicado++;
}
} catch (Exception e) {
// TODO: handle exception
}
}
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;
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this.getApplicationContext(), MainActivity.class), 0);
nt.setLatestEventInfo(this, "Status Replicação 2", "A replicado 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(), MainActivity.class), 0);
nt.setLatestEventInfo(this, "Status Replicação", "Erro na replicação. Total replicado: "+TotalReplicado + " de "+ TotalDB, p);
}
//vibrando
nt.vibrate = new long[]{100,2000,1000,2000};
notificationManager.notify((int)Math.round(Math.random()), nt);
db.close();
}
}
Ola Sidney
coloque logo abaixo de
db.close();
stopSelf();
e coloque aqui o resultado..
O que é stopSelf();
Muito bom, resolveu. Mas não entendi o que ele faz!
sidney,
este comando encerra o service
ele esta travando seu celular pq a service ainda continua rodando
humm, entendi. Obrigado pela ajuda. Abraço!