`
阿尔萨斯
  • 浏览: 4181001 次
社区版块
存档分类
最新评论

service

 
阅读更多

这个例子来自“安卓巴士”,经阅读,理解,写此文章,希望通过这篇一个Demo学完Android中所有的服务对对广大读者有所帮助。

说明:这个例子实现了Android中常见的许多服务,下面是实现的截图

接下来,以源代码的方式分析这个例子

1.MainActivity--主界面

这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:

  1. packagelovefang.stadyService;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.Button;
  5. importandroid.view.View;
  6. importandroid.content.Intent;
  7. importandroid.util.Log;
  8. /**这是使用后台服务的学习例子*/
  9. publicclassMainStadyServicsextendsActivity{
  10. /**参数设置*/
  11. ButtonstartServiceButton;//启动服务按钮
  12. ButtonshutDownServiceButton;//关闭服务按钮
  13. ButtonstartBindServiceButton;//启动绑定服务按钮
  14. ButtonsendBroadcast;//使用广播
  15. ButtonnotificationButton;//使用通知功能
  16. ButtonalarmButton;//使用闹钟
  17. ButtonhandlerButton;//使用handler
  18. ButtonasyncButton;//使用异步加载
  19. ButtonphoneStateButton;//查看手机状态
  20. ButtoncallphoneButton;//拨打电话
  21. ButtonvibratorButton;//使用震动
  22. CountServicecountService;
  23. @Override
  24. publicvoidonCreate(BundlesavedInstanceState){
  25. super.onCreate(savedInstanceState);
  26. Log.v("MainStadyServics","setContentView");
  27. setContentView(R.layout.main);
  28. getWidget();
  29. regiestListener();
  30. }
  31. /**获得组件*/
  32. publicvoidgetWidget(){
  33. startServiceButton=(Button)findViewById(R.id.startServerButton);
  34. startBindServiceButton=(Button)findViewById(R.id.startBindServerButton);
  35. shutDownServiceButton=(Button)findViewById(R.id.sutdownServerButton);
  36. sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
  37. notificationButton=(Button)findViewById(R.id.notification);
  38. alarmButton=(Button)findViewById(R.id.alarm);
  39. handlerButton=(Button)findViewById(R.id.handler);
  40. asyncButton=(Button)findViewById(R.id.async);
  41. phoneStateButton=(Button)findViewById(R.id.phonestate);
  42. callphoneButton=(Button)findViewById(R.id.callphone);
  43. vibratorButton=(Button)findViewById(R.id.vibrator);
  44. }
  45. /**为按钮添加监听*/
  46. publicvoidregiestListener(){
  47. startServiceButton.setOnClickListener(startService);
  48. shutDownServiceButton.setOnClickListener(shutdownService);
  49. startBindServiceButton.setOnClickListener(startBinderService);
  50. sendBroadcast.setOnClickListener(broadcastReceiver);
  51. notificationButton.setOnClickListener(notification);
  52. alarmButton.setOnClickListener(startAlarm);
  53. handlerButton.setOnClickListener(handler);
  54. asyncButton.setOnClickListener(async);
  55. phoneStateButton.setOnClickListener(phonestate);
  56. callphoneButton.setOnClickListener(callphoneEvent);
  57. vibratorButton.setOnClickListener(vibrator);
  58. }
  59. /**启动服务的事件监听*/
  60. publicButton.OnClickListenerstartService=newButton.OnClickListener(){
  61. publicvoidonClick(Viewview){
  62. /**单击按钮时启动服务*/
  63. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  64. startService(intent);
  65. Log.v("MainStadyServics","startService");
  66. }
  67. };
  68. /**关闭服务*/
  69. publicButton.OnClickListenershutdownService=newButton.OnClickListener(){
  70. publicvoidonClick(Viewview){
  71. /**单击按钮时启动服务*/
  72. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  73. /**退出Activity是,停止服务*/
  74. stopService(intent);
  75. Log.v("MainStadyServics","shutDownserveice");
  76. }
  77. };
  78. /**打开绑定服务的Activity*/
  79. publicButton.OnClickListenerstartBinderService=newButton.OnClickListener(){
  80. publicvoidonClick(Viewview){
  81. /**单击按钮时启动服务*/
  82. Intentintent=newIntent(MainStadyServics.this,UseBrider.class);
  83. startActivity(intent);
  84. Log.v("MainStadyServics","startBinderService");
  85. }
  86. };
  87. /**打开广播学习的按钮*/
  88. publicButton.OnClickListenerbroadcastReceiver=newButton.OnClickListener(){
  89. publicvoidonClick(Viewview){
  90. Intentintent=newIntent(MainStadyServics.this,UseBroadcast.class);
  91. startActivity(intent);
  92. Log.v("MainStadyServics","startbroadcast");
  93. }
  94. };
  95. /**打开通知*/
  96. publicButton.OnClickListenernotification=newButton.OnClickListener(){
  97. publicvoidonClick(Viewview){
  98. Intentintent=newIntent(MainStadyServics.this,UseNotification.class);
  99. startActivity(intent);
  100. Log.v("MainStadyService","startNotification");
  101. }
  102. };
  103. /**使用闹钟*/
  104. publicButton.OnClickListenerstartAlarm=newButton.OnClickListener(){
  105. publicvoidonClick(Viewview){
  106. Intentintent=newIntent(MainStadyServics.this,UseAlarmManager.class);
  107. startActivity(intent);
  108. Log.v("MainStadyService","startalarm");
  109. }
  110. };
  111. publicButton.OnClickListenerhandler=newButton.OnClickListener(){
  112. publicvoidonClick(Viewview){
  113. Intentintent=newIntent(MainStadyServics.this,UseHandleMessage.class);
  114. startActivity(intent);
  115. Log.v("MainStadyService","starthandle");
  116. }
  117. };
  118. publicButton.OnClickListenerasync=newButton.OnClickListener(){
  119. publicvoidonClick(Viewview){
  120. Intentintent=newIntent(MainStadyServics.this,UseAsyncTask.class);
  121. startActivity(intent);
  122. Log.v("MainStadyService","starthandle");
  123. }
  124. };
  125. publicButton.OnClickListenerphonestate=newButton.OnClickListener(){
  126. publicvoidonClick(Viewview){
  127. Intentintent=newIntent(MainStadyServics.this,UsePhoneState.class);
  128. startActivity(intent);
  129. Log.v("MainStadyService","startphonestate");
  130. }
  131. };
  132. publicButton.OnClickListenercallphoneEvent=newButton.OnClickListener(){
  133. publicvoidonClick(Viewview){
  134. Intentintent=newIntent(MainStadyServics.this,UseActionCall.class);
  135. startActivity(intent);
  136. Log.v("MainStadyService","startcallphone");
  137. }
  138. };
  139. publicButton.OnClickListenervibrator=newButton.OnClickListener(){
  140. publicvoidonClick(Viewview){
  141. Intentintent=newIntent(MainStadyServics.this,UseVibrator.class);
  142. startActivity(intent);
  143. Log.v("MainStadyService","startcallphone");
  144. }
  145. };
  146. /***/
  147. protectedvoidonDestroy(){
  148. super.onDestroy();
  149. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  150. /**退出Activity是,停止服务*/
  151. stopService(intent);
  152. }
  153. }

2.启动服务按钮

这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志

代码如下:

  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.app.Service;//服务的类
  4. importandroid.os.IBinder;
  5. importandroid.os.Binder;
  6. importandroid.content.Intent;
  7. importandroid.util.Log;
  8. /**计数的服务*/
  9. publicclassCountServiceextendsService{
  10. /**创建参数*/
  11. booleanthreadDisable;
  12. intcount;
  13. publicIBinderonBind(Intentintent){
  14. returnnull;
  15. }
  16. publicvoidonCreate(){
  17. super.onCreate();
  18. /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
  19. newThread(newRunnable(){
  20. publicvoidrun(){
  21. while(!threadDisable){
  22. try{
  23. Thread.sleep(1000);
  24. }catch(InterruptedExceptione){
  25. }
  26. count++;
  27. Log.v("CountService","Countis"+count);
  28. }
  29. }
  30. }).start();
  31. }
  32. publicvoidonDestroy(){
  33. super.onDestroy();
  34. /**服务停止时,终止计数进程*/
  35. this.threadDisable=true;
  36. }
  37. publicintgetConunt(){
  38. returncount;
  39. }
  40. classServiceBinderextendsBinder{
  41. publicCountServicegetService(){
  42. returnCountService.this;
  43. }
  44. }
  45. }

3.绑定服务

服务有两种实现的方法:

(1)startService,启动服务,这时需要程序员管理服务的生命周期

(2)bindService,绑定服务,此时Service与Activity绑定在一起

下面是实现的代码:

  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.app.Activity;
  4. importandroid.content.ComponentName;
  5. importandroid.content.Context;
  6. importandroid.content.Intent;
  7. importandroid.content.ServiceConnection;
  8. importandroid.os.Bundle;
  9. importandroid.os.IBinder;
  10. importandroid.util.Log;
  11. /**通过bindService和unBindSerivce的方式启动和结束服务*/
  12. publicclassUseBriderextendsActivity{
  13. /**参数设置*/
  14. CountServicecountService;
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(newUseBriderFace(this));
  19. Intentintent=newIntent(UseBrider.this,CountService.class);
  20. /**进入Activity开始服务*/
  21. bindService(intent,conn,Context.BIND_AUTO_CREATE);
  22. }
  23. privateServiceConnectionconn=newServiceConnection(){
  24. /**获取服务对象时的操作*/
  25. publicvoidonServiceConnected(ComponentNamename,IBinderservice){
  26. //TODOAuto-generatedmethodstub
  27. countService=((CountService.ServiceBinder)service).getService();
  28. }
  29. /**无法获取到服务对象时的操作*/
  30. publicvoidonServiceDisconnected(ComponentNamename){
  31. //TODOAuto-generatedmethodstub
  32. countService=null;
  33. }
  34. };
  35. protectedvoidonDestroy(){
  36. super.onDestroy();
  37. this.unbindService(conn);
  38. Log.v("MainStadyServics","out");
  39. }
  40. }

接下来为您介绍发送广播、Notification通知、Alarm 闹钟等服务的具体内容

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics