TimerService.java :  » BlueTooth » cyclic-bluetooth-sensor » ddhost » cbs » Android Open Source

Android Open Source » BlueTooth » cyclic bluetooth sensor 
cyclic bluetooth sensor » ddhost » cbs » TimerService.java
package ddhost.cbs;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class TimerService extends Service {
  
  class btTimerBinder extends Binder {
    
    TimerService getService() {
      return TimerService.this;
    }
    
  }
  
  public static final String ACTION = "BT Timer Service";
  private Timer timer;
  
  @Override
  public void onCreate() {
    super.onCreate();
    //Toast toast = Toast.makeText(getApplicationContext(), "onCreate()", Toast.LENGTH_SHORT);
    //toast.show();
    //System.out.println("####### service onCreate() process:"+ android.os.Process.myPid() + " task:" + android.os.Process.myTid());
  }
  
  @Override
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    //Toast toast = Toast.makeText(getApplicationContext(), "onStart()", Toast.LENGTH_SHORT);
    //toast.show();
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    //Toast toast = Toast.makeText(getApplicationContext(), "onDestroy()", Toast.LENGTH_SHORT);
    //toast.show();
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }
  
  @Override
  public IBinder onBind(Intent intent) {
    //Toast toast = Toast.makeText(getApplicationContext(), "onBind()", Toast.LENGTH_SHORT);
    //toast.show();
    return new btTimerBinder();
  }
  
  @Override
  public void onRebind(Intent intent) {
    //Toast toast = Toast.makeText(getApplicationContext(), "onRebind()", Toast.LENGTH_SHORT);
    //toast.show();
  }
  
  @Override
  public boolean onUnbind(Intent intent) {
    //Toast toast = Toast.makeText(getApplicationContext(), "onUnbind()", Toast.LENGTH_SHORT);
    //toast.show();
    return true; //  onRebind  true 
  }
  
  // 
  public void schedule(long delay) {
    if (timer != null) {
      timer.cancel();
    }
    timer = new Timer();
    TimerTask timerTask = new TimerTask() {
      
      public void run() {
        sendBroadcast(new Intent(ACTION));
      }
      
    };
    timer.schedule(timerTask, delay);
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.