实现服务运行小段时间后自己关闭

来源:互联网 发布:highwire press数据库 编辑:程序博客网 时间:2024/05/17 03:27

 showNotification();

  // Start up the thread running the service. Note that we create a
  // separate thread because the service normally runs in the process's
  // main thread, which we don't want to block.
  Thread thr = new Thread(null, mTask, "AlarmService_Service");
  thr.start();

 

 

/**
  * The function that runs in our worker thread
  */
 Runnable mTask = new Runnable()
 {
  public void run()
  {
   // Normally we would do some work here... for our sample, we will
   // just sleep for 30 seconds.
   long endTime = System.currentTimeMillis() + 15 * 1000;
   while (System.currentTimeMillis() < endTime)
   {
    synchronized (mBinder)
    {
     try
     {
      mBinder.wait(endTime - System.currentTimeMillis());
     }
     catch (Exception e)
     {
     }
    }
   }

   // Done with our work... stop the service!
   AlarmService_Service.this.stopSelf();
  }
 };

 

/**
  * This is the object that receives interactions from clients. See
  * RemoteService for a more complete example.
  */
 private final IBinder mBinder = new Binder()
 {
  @Override
  protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException
  {
   return super.onTransact(code, data, reply, flags);
  }
 };

0 0
原创粉丝点击