案例演示

布局文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="${packageName}.${activityClass}" >
  6.  
  7.  <Button
  8.         android:id="@+id/btn_start"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:onClick="click"
  12.         android:text="context.startService" />
  13.  
  14.     <Button
  15.         android:id="@+id/btn_stop"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content"
  18.         android:layout_below="@+id/btn_start"
  19.         android:onClick="click"
  20.         android:text="context.strpService" />
  21.  
  22. </RelativeLayout>

注冊service

  1. <?
  2.  
  3. xml version="1.0" encoding="utf-8"?
  4.  
  5. >
  6. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  7. package="com.example.android_servicedemo"
  8. android:versionCode="1"
  9. android:versionName="1.0" >
  10.  
  11. <uses-sdk
  12. android:minSdkVersion="14"
  13. android:targetSdkVersion="19" />
  14.  
  15. <application
  16. android:allowBackup="true"
  17. android:icon="@drawable/ic_launcher"
  18. android:label="@string/app_name"
  19. android:theme="@style/AppTheme" >
  20. <activity
  21. android:name="com.example.android_servicedemo.MainActivity"
  22. android:label="@string/app_name" >
  23. <intent-filter>
  24. <action android:name="android.intent.action.MAIN" />
  25.  
  26. <category android:name="android.intent.category.LAUNCHER" />
  27. </intent-filter>
  28. </activity>
  29.  
  30. <service
  31. android:name="ServiceTese"
  32. android:permission="com.example.permission.servers" >
  33. <intent-filter>
  34. <action android:name="com.example.serversdemo.action" />
  35. </intent-filter>
  36. </service>
  37. </application>
  38.  
  39. </manifest>

主activity

  1. package com.example.android_servicedemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.Service;
  5. import android.content.ComponentName;
  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. import android.view.View;
  12.  
  13. public class MainActivity extends Activity {
  14. private Intent service;
  15. MyConn myConn;
  16. public static final String TAG = "MainActivity";
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22.  
  23. // 隐士意图的启动
  24. service = new Intent();
  25. service.setAction("com.example.serversdemo.action");
  26. }
  27.  
  28. public void click(View v) {
  29. int id = v.getId();
  30. switch (id) {
  31. /*
  32. * 启动服务
  33. */
  34. case R.id.btn_start:
  35. this.startService(service);
  36. break;
  37. /*
  38. * 停止服务
  39. */
  40. case R.id.btn_stop:
  41. this.stopService(service);
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47.  
  48. }

执行结果

点击context.startService--->点击context.stopService。

生命周期为

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

点击context.startService--->home键--->点击context.startService--->点击context.startService...--->点击context.stopService


onbindService见http://blog.csdn.net/zhaoyazhi2129/article/details/32712073

赵雅智:service_startService生命周期的更多相关文章

  1. 赵雅智_Fragment生命周期

    官网帮助文档链接:  http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...

  2. 赵雅智_service生命周期

    Android中的服务和windows中的服务是类似的东西,服务一般没实用户操作界面.它执行于系统中不easy被用户发觉,能够使用它开发如监控之类的程序. 服务的开发步骤 第一步:继承Service类 ...

  3. 赵雅智:android教学大纲

    带下划线为详细内容链接地址.点击后可跳转.希望给大家尽一些微薄之力.眼下还在整理中 教学章节 教学内容 学时安排 备注 1 Android高速入门 2 Android模拟器与常见命令 3 Androi ...

  4. 赵雅智:service_bindService生命周期

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  5. 赵雅智_BroadcastReceiver

    BroadcastReceiver  用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...

  6. 赵雅智_Swift(1)_swift简单介绍及类型

    Swift 是 iOS 和 OS X 应用开发的一门新语言. 假设你有 C 或者 Objective-C 开发经验, Swift 的非常多内容都是你熟悉的. Swift 的类型是在 C 和 Objec ...

  7. 赵雅智_Fragment

    当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块. 在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就能 ...

  8. 赵雅智_ContentProvider

    ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...

  9. 赵雅智:js知识点汇总

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...

随机推荐

  1. jQuery Validate W3C内容

    导入 js 库 <script src="../js/jquery.js" type="text/javascript"></script&g ...

  2. RecyclerView 小记

    RecyclerView,是在v7包加入的,一个灵活的view可以展示巨大的数据集,类似于listview的viewholder复用已经优化好了. 语言是苍白的,代码是最生动的叙说: 布局: < ...

  3. 服务器响应HTTP请求状态码(转)

    当服务器响应HTTP请求时,其状态行的信息为HTTP的版本号,状态码,及解释状态码的简单说明: 1.客户方错误: 100 客户必须继续发出请求 101 客户要求服务器根据请求转换HTTP协议版本 2. ...

  4. codeblock编译Object-C

    http://blog.csdn.net/ldl22847/article/details/7482971 http://www.cnblogs.com/qingyuan/p/3524791.html ...

  5. FTL(Flash translation layer)闪存转换层

    前面说过,闪存的读写单位为页,而页的大小一般为4KB或8KB,但我们的操作系统读写数据是按HDD的扇区尺寸进行的(512Byte(字节)),更麻烦的是闪存擦除以块作单位,而且未擦除就无法写入,这导致操 ...

  6. 红外摄像头为什么使用850nm波长红外发射管

    市面上有很多不同波长的红外发射管,其中以850nm和940nm波长为主.那么红外摄像头为什么使用850nm波长红外发射管? 首先,我们来了解一下红外摄像头的相关知识.简单来说红外摄像头是用来感应红外线 ...

  7. Azure 网站上的 Java

     编辑人员注释:本文章由Windows Azure 网站团队的项目经理Chris Compy 撰写. Microsoft 已推出针对 Azure 网站上基于 Java 的网站的支持.此功能旨在通过 ...

  8. CDialogSK - A Skinnable Dialog Class

    Introduction This class is derived from the MFC CDialog. It supports the following features :- If ru ...

  9. delphi ICS控件示例解读

    {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Author: Fran鏾is PIETTE ...

  10. Java 根据comboBox选择结果显示JTable

    处理这样的问题的主要思路是:     对于JTable,JTree等Swing控件,都有一个对应的Model用来存储数据,JTable对应的有一个DefaultTableModel.     Defa ...