I make a receiver that work, and this is the code:
public class ServicioCargado extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("domain.androids.smsprojects.MyService");
context.startService(i);
}
}
}
and this is my service, It is created by the receiver but is close automatically, and I dont want that behavior, I want the timer to keep running, and execute it every 5 seconds, I dont see the problem, thank for all
public class MyService extends Service {
private MyService actual=null;
private Timer timer = new Timer();
private static final long UPDATE_INTERVAL = 5000;
//private final IBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Hacemos un while que cada cierto tiempo haga algo o un sleep
Toast.makeText(this,"Se esta ejecutando el programa para enviar sms", Toast.LENGTH_SHORT).show();
actual=this;
Log.d("Mauricio", "Creando servicio");
Toast.makeText(this,"Antes de ejecut开发者_高级运维ar el timer", Toast.LENGTH_SHORT).show();
MyTimerExecution();
Toast.makeText(this,"Despues de ejecutar el timer", Toast.LENGTH_SHORT).show();
}
private void MyTimerExecution() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Imagine here a freaking cool web access ;-)
//Toast.makeText(actual,"Service update ...", Toast.LENGTH_SHORT).show();
Log.d("Mauricio","aqui");
}
}, 0, UPDATE_INTERVAL);
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}
try Handler instead ,call sendMessageDelayed(msg,UPDATE_INTERVAL)
after what you really want to do.
精彩评论