2011-09-18 11 views
1

によってコール玉葉サービスは、私が仕事受信機を作り、これがコードであることを、タイマーをサービス内のすべての5秒を使用する:受信機

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); 
     } 
    } 

} 

と、これは私のサービスであり、それが作成され受信機ではなく、自動的に近く、私はその行動をたくない、私はタイマーは5秒ごとに実行し続けると、それを実行したい、私は問題を参照してくださいいけない、すべての

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 ejecutar 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(); 
    } 
} 

答えて

1

に感謝、代わりにハンドラを試してみてくださいあなたが本当にやりたいことの後にsendMessageDelayed(msg,UPDATE_INTERVAL)に電話してください。