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

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. }
复制代码

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

相关推荐

    Android Studio蓝牙通信客户端Demo源码BTClient.rar

    在这个“Android Studio蓝牙通信客户端Demo源码BTClient.rar”中,我们可以看到一个实现Android设备之间通过蓝牙进行通信的客户端示例。这个Demo适用于那些想要学习或集成蓝牙通信功能到自己应用的开发者。 蓝牙...

    Android ImageButton图片按钮Demo源码.rar

    这个"Android ImageButton图片按钮Demo源码.rar"应该包含了一个完整的示例项目,用于演示如何在Android应用中使用`ImageButton`。 首先,我们来详细了解一下`ImageButton`的基本用法。`ImageButton`主要通过设置其`...

    Android 绘制贝塞尔曲线Demo源码.rar

    在Android开发中,贝塞尔曲线是一种非常常见的图形...这个Demo源码将提供一个直观的例子,帮助开发者理解和实践如何在Android中运用贝塞尔曲线,通过学习和分析源码,你可以掌握其核心原理,并将其应用到自己的项目中。

    Android剪切图动画Demo源码.rar

    此“Android剪切图动画Demo源码.rar”包含了一个实际的项目示例,可以帮助开发者深入理解如何在Android应用中实现此类动画。 剪切图动画主要涉及以下知识点: 1. **动画系统**:Android提供了两种主要的动画系统:...

    android demo 源码

    下面我们将深入探讨Android Demo源码中的关键知识点。 1. **Activity和Intent**:Activity是Android应用的基本组件,代表用户界面的一个屏幕。Intent用于启动Activity或在Activity之间传递数据。在Demo中,我们通常...

    android 便签 Demo,完整源码

    总的来说,“android 便签 Demo”项目涵盖了Android开发中的一些核心知识点,如SQLite数据库操作、ListView使用、UI组件(如FloatingActionButton)的设计以及Intent的使用。通过这个项目,开发者可以深入理解...

    Android悬浮窗Demo源码-悬浮球转盘、悬浮加速小火箭效果、悬浮视频图片播放

    源码实现了三种比较常用的悬浮框效果 1.悬浮球转盘效果 2.悬浮加速小火箭效果 3.悬浮播放视频图片效果 具体实现思路和效果图可以参考博文 https://blog.csdn.net/daokedream/article/details/132740710

    Android应用源码 ListView下拉刷新 Demo

    这个"Android应用源码 ListView下拉刷新 Demo"提供了一个实际的例子,帮助开发者了解如何在ListView中实现这一功能。 1. **SwipeRefreshLayout**:Android SDK 提供了一个名为SwipeRefreshLayout的布局容器,它是...

    Android RadioButton与监听Demo源码.rar

    本教程将深入探讨Android中的RadioButton及其监听机制,通过提供的Demo源码,我们可以更直观地了解其工作原理。 1. **RadioButton基本用法** - `RadioButton` 是 `CompoundButton` 类的子类,继承自 `Button`。它...

    安卓Android源码——360全景查看demo.rar

    【Android 源码——360全景查看 Demo】是一个专为Android平台设计的源代码示例,旨在帮助开发者理解和实现360度全景图像的查看功能。在Android开发中,这种技术通常用于虚拟现实(VR)应用或者增强现实(AR)场景,...

    8:Android项目源码-毕业设计源码(168套).rar

    android常用图片特效处理.zip android超炫的图片浏览器.zip Android——仿美图秀秀和IOS系统的相机胶卷.zip Android创建UI的新思路:用javascript与Activity进行交互.zip Android模仿易网新闻页面源码(异步加载).zip...

    Accessibility安卓无障碍服务源码

    总的来说,这个“Accessibility安卓无障碍服务源码”项目为开发者提供了一个快速入门无障碍服务的起点,通过学习和理解这个源码,你可以更深入地掌握Android无障碍服务的原理和实践,从而实现更多定制化的功能。...

    安卓Android源码——进度条对话框Demo源码.zip

    这个"安卓Android源码——进度条对话框Demo源码.zip"文件包含了一个完整的示例项目,用于演示如何在Android应用程序中实现进度条对话框。 首先,`.classpath`文件是Eclipse(一种常用的Android开发IDE)的工作空间...

    安卓Android源码——多种控件的Demo.zip

    《安卓Android源码——多种控件的Demo》 在安卓应用开发中,理解并熟练运用各种控件是至关重要的。这份"安卓Android源码——多种控件的Demo"提供了丰富的实例,帮助开发者深入理解Android UI组件的使用。下面将详细...

    curl在Android中使用的Demo

    在Android开发中,`curl`常被用来调试网络请求,理解API接口的工作方式,或者快速验证网络服务的响应。这篇博客文章《curl在Android中使用的Demo》将向我们展示如何在Android应用中集成和使用`curl`命令。 首先,...

    Android 登录界面Demo源码.zip

    这份"Android登录界面Demo源码"提供了一个很好的学习资源,帮助开发者理解如何在Android平台上实现一个功能完整的登录界面。下面我们将详细探讨这个项目中的关键知识点。 1. **XML布局设计**: - 使用XML布局文件...

    android中常用的比较深入的一些用途demo

    本文将深入探讨在Android开发中常用的一些高级技术,包括Async Task异步任务、蓝牙编程、Handler机制以及对Framework源码的理解。这些知识点对于提升Android开发者的技能水平至关重要。 首先,Async Task是Android...

    Android相机调用和自定义相机Demo源码

    在Android开发中,相机功能是不可或缺的一部分,无论是用于拍摄照片还是录制视频,它都是移动应用中的常用特性。本文将深入探讨如何在Android中调用系统...希望这个Demo源码能帮助你更好地理解和实践Android相机功能。

    安卓(android)离线人脸识别Demo源码

    然而,这个"安卓(android)离线人脸识别Demo源码"项目提供了一个解决方案,它实现了在本地设备上进行人脸检测、人脸对齐、人脸库构建以及人脸识别等一系列功能,无需依赖云端服务。 1. **人脸检测**:人脸检测是识别...

    Android Rss订阅源码Demo.zip

    本示例是关于如何在Android应用中实现RSS订阅功能的源码Demo。通过分析这个Demo,我们可以深入理解Android应用如何处理XML解析、网络请求以及UI更新等多个关键知识点。 首先,RSS订阅的核心在于解析RSS Feed,通常...

Global site tag (gtag.js) - Google Analytics