胡杨林 2012-09-14
<service android:name=".mService1" android:exported="true" android:process=":remote" ></service>
/*自定义mService类继承Service类*/
publicclassmService1extendsService
{
/*建立Handler对象,作为线程传递postDelayed之用*/
privateHandlerobjHandler=newHandler();
/*为确认系统服务执行情况*/
privateintintCounter=0;
/*成员变量mTasks为Runnable对象,作为Timer之用*/
privateRunnablemTasks=newRunnable()
{
/*执行线程*/
publicvoidrun()
{
/*递增counter整数,作为后台服务运行时间识别*/
intCounter++;
/*以Log对象LogCat里输出log信息,监看服务执行情况*/
Log.i("HIPPO","Counter:"+Integer.toString(intCounter));
/*每1秒调用Handler.postDelayed方法反复执行*/
objHandler.postDelayed(mTasks,1000);
}
};
@Override
publicvoidonStart(Intentintent,intstartId)
{
//TODOAuto-generatedmethodstub
super.onStart(intent,startId);
}@Override
publicvoidonCreate()
{
//TODOAuto-generatedmethodstub
/*服务开始,调用每1秒mTasks线程*/
objHandler.postDelayed(mTasks,1000);
super.onCreate();
}
@Override
publicvoidonDestroy()
{
//TODOAuto-generatedmethodstub
/*当服务结束,移除mTasks线程*/
objHandler.removeCallbacks(mTasks);
super.onDestroy();
}
}