`
844604778
  • 浏览: 550648 次
文章分类
社区版块
存档分类
最新评论

Android常用的一些服务demo源码

 
阅读更多
今天在网站看了一系列例子。太棒了。。。
我收藏了哦。
实现了Android中常见的许多服务,下面是实现的截图

1.png

接下来,以源代码的方式分析这个例子
1.MainActivity--主界面
这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:

  1. package lovefang.stadyService;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.view.View;
  6. import android.content.Intent;
  7. import android.util.Log;
  8. /**这是使用后台服务的学习例子*/
  9. public class MainStadyServics extends Activity {
  10. /**参数设置*/
  11. Button startServiceButton;// 启动服务按钮
  12. Button shutDownServiceButton;// 关闭服务按钮
  13. Button startBindServiceButton;// 启动绑定服务按钮
  14. Button sendBroadcast;// 使用广播
  15. Button notificationButton;// 使用通知功能
  16. Button alarmButton;// 使用闹钟
  17. Button handlerButton;// 使用handler
  18. Button asyncButton;// 使用异步加载
  19. Button phoneStateButton;// 查看手机状态
  20. Button callphoneButton;// 拨打电话
  21. Button vibratorButton;// 使用震动
  22. CountService countService;

  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. Log.v("MainStadyServics", "setContentView");
  27. setContentView(R.layout.main);
  28. getWidget();
  29. regiestListener();
  30. }
  31. /**获得组件*/
  32. public void getWidget(){
  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. public void regiestListener(){
  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. public Button.OnClickListener startService = new Button.OnClickListener(){
  61. public void onClick(View view){
  62. /**单击按钮时启动服务*/
  63. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  64. startService(intent);
  65. Log.v("MainStadyServics", "start Service");
  66. }
  67. };
  68. /**关闭服务*/
  69. public Button.OnClickListener shutdownService = new Button.OnClickListener(){
  70. public void onClick(View view){
  71. /**单击按钮时启动服务*/
  72. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  73. /**退出Activity是,停止服务*/
  74. stopService(intent);
  75. Log.v("MainStadyServics", "shutDown serveice");
  76. }
  77. };
  78. /**打开绑定服务的Activity*/
  79. public Button.OnClickListener startBinderService = new Button.OnClickListener(){
  80. public void onClick(View view){
  81. /**单击按钮时启动服务*/
  82. Intent intent = new Intent(MainStadyServics.this,UseBrider.class);
  83. startActivity(intent);
  84. Log.v("MainStadyServics", "start Binder Service");
  85. }
  86. };
  87. /**打开广播学习的按钮*/
  88. public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
  89. public void onClick(View view){
  90. Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);
  91. startActivity(intent);
  92. Log.v("MainStadyServics","start broadcast");
  93. }
  94. };
  95. /**打开通知*/
  96. public Button.OnClickListener notification = new Button.OnClickListener(){
  97. public void onClick(View view){
  98. Intent intent = new Intent(MainStadyServics.this, UseNotification.class);
  99. startActivity(intent);
  100. Log.v("MainStadyService ","start Notification");

  101. }
  102. };
  103. /**使用闹钟*/
  104. public Button.OnClickListener startAlarm = new Button.OnClickListener(){
  105. public void onClick(View view){
  106. Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);
  107. startActivity(intent);
  108. Log.v("MainStadyService ","start alarm");

  109. }
  110. };
  111. public Button.OnClickListener handler= new Button.OnClickListener(){
  112. public void onClick(View view){
  113. Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);
  114. startActivity(intent);
  115. Log.v("MainStadyService ","start handle");
  116. }
  117. };
  118. public Button.OnClickListener async= new Button.OnClickListener(){
  119. public void onClick(View view){
  120. Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);
  121. startActivity(intent);
  122. Log.v("MainStadyService ","start handle");
  123. }
  124. };
  125. public Button.OnClickListener phonestate= new Button.OnClickListener(){
  126. public void onClick(View view){
  127. Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);
  128. startActivity(intent);
  129. Log.v("MainStadyService ","start phonestate");
  130. }
  131. };
  132. public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){
  133. public void onClick(View view){
  134. Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);
  135. startActivity(intent);
  136. Log.v("MainStadyService ","start callphone");
  137. }
  138. };
  139. public Button.OnClickListener vibrator= new Button.OnClickListener(){
  140. public void onClick(View view){
  141. Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);
  142. startActivity(intent);
  143. Log.v("MainStadyService ","start callphone");
  144. }
  145. };
  146. /***/
  147. protected void onDestroy(){
  148. super.onDestroy();
  149. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  150. /**退出Activity是,停止服务*/
  151. stopService(intent);
  152. }


  153. }
复制代码


2.启动服务按钮这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志代码如下:
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.app.Service;// 服务的类
  4. import android.os.IBinder;
  5. import android.os.Binder;
  6. import android.content.Intent;
  7. import android.util.Log;
  8. /**计数的服务*/
  9. public class CountService extends Service{
  10. /**创建参数*/
  11. boolean threadDisable ;
  12. int count;

  13. public IBinder onBind(Intent intent){
  14. return null;
  15. }
  16. public void onCreate(){
  17. super.onCreate();
  18. /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
  19. new Thread(new Runnable(){
  20. public void run(){
  21. while(!threadDisable){
  22. try{
  23. Thread.sleep(1000);
  24. }catch(InterruptedException e){

  25. }
  26. count++;
  27. Log.v("CountService","Count is"+count);
  28. }
  29. }
  30. }).start();
  31. }
  32. public void onDestroy(){
  33. super.onDestroy();
  34. /**服务停止时,终止计数进程*/
  35. this.threadDisable = true;
  36. }
  37. public int getConunt(){
  38. return count;
  39. }
  40. class ServiceBinder extends Binder{
  41. public CountService getService(){
  42. return CountService.this;
  43. }
  44. }
  45. }
复制代码
3.绑定服务服务有两种实现的方法:1.startService,启动服务,这时需要程序员管理服务的生命周期2.bindService,绑定服务,此时Service与Activity绑定在一起下面是实现的代码:
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.util.Log;

  11. /**通过bindService和unBindSerivce的方式启动和结束服务*/
  12. public class UseBrider extends Activity {
  13. /**参数设置*/
  14. CountService countService;

  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(new UseBriderFace(this));
  19. Intent intent = new Intent(UseBrider.this,CountService.class);
  20. /**进入Activity开始服务*/
  21. bindService(intent, conn, Context.BIND_AUTO_CREATE);

  22. }
  23. private ServiceConnection conn = new ServiceConnection(){
  24. /**获取服务对象时的操作*/
  25. public void onServiceConnected(ComponentName name, IBinder service) {
  26. // TODO Auto-generated method stub
  27. countService = ((CountService.ServiceBinder)service).getService();

  28. }
  29. /**无法获取到服务对象时的操作*/
  30. public void onServiceDisconnected(ComponentName name) {
  31. // TODO Auto-generated method stub
  32. countService =null;
  33. }


  34. };
  35. protected void onDestroy(){
  36. super.onDestroy();
  37. this.unbindService(conn);
  38. Log.v("MainStadyServics", "out");
  39. }
  40. }
复制代码
4.发送广播使用sendBroadcast,向一个Action发送广播,并由相应的广播接收器接收并执行相应的动作实现的代码如下:4.1 打开广播服务
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.view.View;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.widget.Button;
  8. /**使用Broadcast,这是一个发送广播的类*/
  9. public class UseBroadcast extends Activity{
  10. /**创建参数*/
  11. private Button sendBroadcast;
  12. /**创建Activity*/
  13. public void onCreate(Bundle savedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.broadcast);// 使用布局文件
  16. getView();
  17. sendBroadcast.setOnClickListener(sendBroadcastClick);// 添加事件监听
  18. }
  19. public void getView(){
  20. sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
  21. }
  22. /**创建事件监听*/
  23. public Button.OnClickListener sendBroadcastClick = new Button.OnClickListener(){
  24. public void onClick(View view){
  25. Intent intent = new Intent();// 创建意图
  26. intent.putExtra("CONTENT","This is a Braodcast demo");// 设置广播的内容
  27. intent.setAction("lovefang.stadyService");// 设置广播的Action
  28. sendBroadcast(intent);
  29. }
  30. };

  31. }
复制代码

未完,待续。。。。。。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics