mService1.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:2k
- package irdc.ex06_15;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.IBinder;
- import android.util.Log;
- public class mService1 extends Service
- {
- /* 创建Handler对象 */
- private Handler objHandler = new Handler();
-
- /* 服务里的递增整数counter */
- private int intCounter=0;
-
- /* 自定义要过滤的广播信息(DavidLanz) */
- public static final String HIPPO_SERVICE_IDENTIFIER="DavidLanz";
-
- /* 运行线程Tasks每1秒运行一次 */
- private Runnable mTasks = new Runnable()
- {
- public void run()
- {
- intCounter++;
-
- /* 当背景Service运行5秒后,送出自定义的广播信息 */
- if(intCounter==5)
- {
- /* DavidLanz */
- Intent i = new Intent(HIPPO_SERVICE_IDENTIFIER);
-
- /* 通过putExtra方法封装参数返回Activity */
- i.putExtra
- (
- "STR_PARAM1",
- getResources().getText
- (R.string.str_message_from_service).toString()
- );
- sendBroadcast(i);
- }
-
- /* 以Log于console里输出作为追踪服务之用 */
- Log.i("HIPPO", "Service Running Counter:"+
- Integer.toString(intCounter));
-
- objHandler.postDelayed(mTasks, 1000);
- }
- };
-
- @Override
- public void onStart(Intent intent, int startId)
- {
- // TODO Auto-generated method stub
-
- /* 服务开始运行,启用运行线程 */
- objHandler.postDelayed(mTasks, 1000);
- super.onStart(intent, startId);
- }
- @Override
- public void onCreate()
- {
- // TODO Auto-generated method stub
- super.onCreate();
- }
-
- @Override
- public IBinder onBind(Intent intent)
- {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void onDestroy()
- {
- // TODO Auto-generated method stub
-
- /* 关闭服务时,关闭运行线程 */
- objHandler.removeCallbacks(mTasks);
- super.onDestroy();
- }
- }