源码

  1. public class ServiceDemoActivity extends Activity {
  2. private static final String TAG = "ServiceDemoActivity";
  3.  
  4. Button bindBtn;
  5. Button startBtn;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10.  
  11. bindBtn = (Button)findViewById(R.id.bindBtn);
  12. startBtn = (Button)findViewById(R.id.startBtn);
  13.  
  14. bindBtn.setOnClickListener(new OnClickListener() {
  15. public void onClick(View v) {
  16. Log.e(TAG, "bindBtn");
  17. bindService(new Intent(ServiceDemo.ACTION), conn, BIND_AUTO_CREATE);
  18. }
  19. });
  20.  
  21. startBtn.setOnClickListener(new OnClickListener() {
  22. public void onClick(View v) {
  23. Log.e(TAG, "startBtn");
  24. startService(new Intent(ServiceDemo.ACTION));
  25. }
  26. });
  27. }
  28.  
  29. ServiceConnection conn = new ServiceConnection() {
  30. public void onServiceConnected(ComponentName name, IBinder service) {
  31. Log.e(TAG, "onServiceConnected");
  32. }
  33. public void onServiceDisconnected(ComponentName name) {
  34. Log.e(TAG, "onServiceDisconnected");
  35. }
  36. };
  37. @Override
  38. protected void onDestroy() {
  39. Log.e(TAG, "onDestroy unbindService");
  40. unbindService(conn);
  41. super.onDestroy();
  42. };
  43. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <Button
  8. android:id="@+id/bindBtn"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="绑定服务" />
  12.  
  13. <Button
  14. android:id="@+id/startBtn"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="启动服务" />
  18.  
  19. </LinearLayout>
  1. public class ServiceDemo extends Service {
  2. private static final String TAG = "ServiceDemo";
  3. public static final String ACTION = "com.example.servicedemoactivity.ServiceDemo";
  4.  
  5. @Override
  6. public IBinder onBind(Intent arg0) {
  7. // TODO Auto-generated method stub
  8. Log.e(TAG, " onBind ");
  9. return null;
  10. }
  11.  
  12. @Override
  13. public void onCreate() {
  14. // TODO Auto-generated method stub
  15. Log.e(TAG, " onCreate ");
  16. super.onCreate();
  17. }
  18.  
  19. @Override
  20. public void onStart(Intent intent, int startId) {
  21. // TODO Auto-generated method stub
  22. Log.e(TAG, " 1onStart ");
  23. super.onStart(intent, startId);
  24. }
  25.  
  26. @Override
  27. public int onStartCommand(Intent intent, int flags, int startId) {
  28. // TODO Auto-generated method stub
  29. Log.e(TAG, " onStartCommand ");
  30. return super.onStartCommand(intent, flags, startId);
  31. }
  32.  
  33. }
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.example.servicedemoactivity"
  3. android:versionCode="1"
  4. android:versionName="1.0" >
  5.  
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9.  
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15.  
  16. <activity android:name=".ServiceDemoActivity">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN"/>
  19. <category android:name="android.intent.category.LAUNCHER"/>
  20. </intent-filter>
  21. </activity>
  22.  
  23. <service android:name="com.example.servicedemoactivity.ServiceDemo">
  24. <intent-filter >
  25. <action android:name="com.example.servicedemoactivity.ServiceDemo"/>
  26. </intent-filter>
  27. </service>
  28.  
  29. </application>
  30.  
  31. </manifest>

参考  http://aswang.iteye.com/blog/1424309

android 中service的简单事例的更多相关文章

  1. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  2. Android中Service的使用详解和注意点(LocalService)

    Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...

  3. Android中Service的一个Demo例子

    Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Servic ...

  4. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

  5. Android中Service的使用

    我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...

  6. Android中Service深入学习

    概述 1.当用户在与当前应用程序不同的应用程序时,Service可以继续在后台运行. 2.Service可以让其他组件绑定,以便和它交互并进行进程间通信. 3.Service默认运行在创建它的应用程序 ...

  7. Android中Service与多个Activity通信

    由于项目需要,我们有时候需要在service中处理耗时操作,然后将结果发送给activity以更新状态.通常情况下,我们只需要在一个service与一个activity之间通信,通常这种情况下,我们使 ...

  8. Android中Service概述

    Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...

  9. android中的回调简单认识

    首先说一下最抽象的形式--2个类,A类和B类.A类含有1个接口.1个接口变量.(可能含有)1个为接口变量赋值的方法以及1个会使用接口变量的"地方";B类实现A中的接口,(可能)含有 ...

  10. Android中Service和Activity之间的通信

    启动Service并传递数据进去: Android中通过Intent来启动服务会传递一个Intent过去. 可以在Intent中通过putExtra()携带数据 Intent startIntent ...

随机推荐

  1. Kubernetes(k8s)访问控制:权限管理之RBAC鉴权

    目录 一.系统环境 二.前言 三.Kubernetes访问控制 四.鉴权简介 五.配置客户端机器 六.设置k8s集群允许所有请求访问 七.设置k8s集群拒绝所有请求访问 八.RBAC授权 8.1 ro ...

  2. Go 语言 context 都能做什么?

    原文链接: Go 语言 context 都能做什么? 很多 Go 项目的源码,在读的过程中会发现一个很常见的参数 ctx,而且基本都是作为函数的第一个参数. 为什么要这么写呢?这个参数到底有什么用呢? ...

  3. Git存储

    Git还提供了一个贮藏的功能.如果你某个分支开发过程中,这个分支的内容是要在本月月底上线的,但是生产上已经出现了一个重大bug,需要你立马去修复.你在分支开发的内容已经开发一部分了,工作区有内容是不能 ...

  4. 图扑 AR 技术应用与管理:施工建造、机柜扫描、办公室导航解决方案

    随着科技的不断革新和创新,越来越多的行业开始迎来数字化时代的变革.建筑行业作为人类历史上最重要的产业之一,在数字化转型方面同样也在不断推进.图扑软件结合 AR 技术的应用,为建筑行业带来了更加便捷高效 ...

  5. Java IO流 - 字节流的使用详细介绍

    IO流的基本介绍: IO流的概述: i 表示intput,是数据从硬盘文件读入到内存的过程,称之输入,负责读. o 表示output,是内存程序的数据从内存到写出到硬盘文件的过程,称之输出,负责写. ...

  6. [HUBUCTF 2022 新生赛]simple_RE

    [HUBUCTF 2022 新生赛]simple_RE 查壳,64位 找main函数,F5查看伪代码,简单分析一下 int __cdecl main(int argc, const char **ar ...

  7. 【转载】Linux虚拟化KVM-Qemu分析(三)之KVM源码(1)

    原文信息: 作者:LoyenWang 出处:https://www.cnblogs.com/LoyenWang/ 公众号:LoyenWang 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作 ...

  8. C#中DataTable的一些使用(后续继续补充)

    C#中DataTable的一些使用 新建一个DataTable DataTable table = new DataTable(); table.Columns.Add("姓名", ...

  9. 这可能是前端处理excel最好的工具了

    大家好,我是程序视点的小二哥! 今天小二哥要分享的是一个纯前端实现读取和导出excel文件的工具库:ExcelJS ExcelJs 简介 功能十分简单: 读取,操作并写入电子表格数据和样式到 XLSX ...

  10. 26种source-map看花了眼?别急,理解这几个全弄懂

    上一篇 webpack处理模块化源码 的文章中提到了 "source map",这一篇来详细说说. 有什么作用 source map 用于映射编译后的代码与源码,这样如果编译后的代 ...