源码

public class ServiceDemoActivity extends Activity {
private static final String TAG = "ServiceDemoActivity"; Button bindBtn;
Button startBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); bindBtn = (Button)findViewById(R.id.bindBtn);
startBtn = (Button)findViewById(R.id.startBtn); bindBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "bindBtn");
bindService(new Intent(ServiceDemo.ACTION), conn, BIND_AUTO_CREATE);
}
}); startBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "startBtn");
startService(new Intent(ServiceDemo.ACTION));
}
});
} ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "onServiceDisconnected");
}
};
@Override
protected void onDestroy() {
Log.e(TAG, "onDestroy unbindService");
unbindService(conn);
super.onDestroy();
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/bindBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" /> <Button
android:id="@+id/startBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动服务" /> </LinearLayout>
public class ServiceDemo extends Service {
private static final String TAG = "ServiceDemo";
public static final String ACTION = "com.example.servicedemoactivity.ServiceDemo"; @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.e(TAG, " onBind ");
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
Log.e(TAG, " onCreate ");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.e(TAG, " 1onStart ");
super.onStart(intent, startId);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e(TAG, " onStartCommand ");
return super.onStartCommand(intent, flags, startId);
} }
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicedemoactivity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <activity android:name=".ServiceDemoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity> <service android:name="com.example.servicedemoactivity.ServiceDemo">
<intent-filter >
<action android:name="com.example.servicedemoactivity.ServiceDemo"/>
</intent-filter>
</service> </application> </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. 痞子衡嵌入式:主流QuadSPI NOR Flash厂商关于QE位与IO功能复用关联设计

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是几家主流QuadSPI NOR Flash厂商关于QE位与IO功能复用关联设计. 痞子衡之前写过一篇文章 <串行NOR Flash下 ...

  2. Spring Cloud灰度部署

    1.背景(灰度部署) 在我们系统发布生产环境时,有时为了确保新的服务逻辑没有问题,会让一小部分特定的用户来使用新的版本(比如客户端的内测版本),而其余的用户使用旧的版本,那么这个在Spring Clo ...

  3. C#使用企业微信群机器人推送生产数据

    在日常的工作生产中,经常会有将将生产数据或者一些信息主动推送给相关的管理人员,我们公司在开发WMS系统时,为了仓库的储存安全,需要在危废品库存达到一定的储量时,自动通知仓管员去处理危废品,所以就需要程 ...

  4. Taurus .Net Core 微服务开源框架:Admin 插件【1】 - 微服务节点管理

    前言: 最近发现 NetCore 的文章有点少,特来补几篇. 上一篇:Taurus.mvc .Net Core 微服务开源框架发布V3.1.7:让分布式应用更高效. 自上篇之后,期间更新了4个小版本, ...

  5. 记一次 .NET 某企业采购平台 崩溃分析

    一:背景 1. 讲故事 前段时间有个朋友找到我,说他们的程序有偶发崩溃的情况,让我帮忙看下怎么回事,针对这种 crash 的程序,用 AEDebug 的方式抓取一个便知,有了 dump 之后接下来就可 ...

  6. FHQ-Treap的详细图解

    第一部分 按值分裂的 FHQ-Treap 按值分裂的 FHQ-Treap 的典型例题是P3369 [模板]普通平衡树. 思路 FHQ-Treap 是什么? FHQ-Treap 是二叉搜索树的一种. 比 ...

  7. Java开发大型互联网-架构师必须掌握的分布式技术

    Java开发大型互联网-架构师必须掌握的分布式技术 摘要:在当今互联网行业,随着用户量和业务的不断增长,大型互联网系统的设计和开发已经成为了一项头等重要的任务.作为架构师,要能够应对这样的挑战,就必须 ...

  8. Redis的设计与实现(4)-跳跃表

    跳跃表 (skiplist) 是一种有序数据结构, 它通过在每个节点中维持多个指向其他节点的指针, 从而达到快速访问节点的目的. 跳跃表支持平均 O(log N) 最坏 O(N) 复杂度的节点查找, ...

  9. node:windows script host 錯誤 console未定义

    错误背景 在开发npm包时,碰到此项报错 解决方案 选中任意js文件,选择打开方式,指定到node中即可

  10. Django: You are trying to add a non-nullable field 'name' to mainnav without a default; we can't do that (the database needs something to populate existing rows).

    错误原因: 语句中缺少默认值 class Main(models.Model): img = models.CharField(max_length=255) name = models.CharFi ...