Android 应用开发中对于那些运行在主线并且执行时间较长的后台服务,为了避免应用发生无响应异常而崩溃,通常会在 Service 类的内部创建一个单独的线程,如下面程序片断,请逐行注释该程序片断,并简要说明其功能。 public int onStartCommand(Intent intent, int falg, int startid){ Log.v("CountService", "onStartCommand"); new Thread(new Runnable(){ public void run() { while(!threadDisable){ try{ Thread.sleep(1000); }catch(InterruptedException e){ } count++; Log.v("CountService", "Count is " + count); } } }).start(); return START_STICKY; }