源码

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. ChatGPT:免费在线聊天网页版,探索智能人机交互的便捷新方式!

    当今,机器智能相当流行.而在线人工智能聊天系统的兴起大大改变了我们与计算机互动的方式.本文将介绍一款名为 ChatGPT 的在线免费智能聊天网页版,让你体验智能对话的便利性. ChatGPT 是一种基 ...

  2. Cisco命令中login和login local的区别

    login是开启远程登录密码验证,login local不但要求密码,还要求提供用户名 如果同时设置login和login local,login local有效 (config-line)#line ...

  3. 【小小demo】Springboot + Vue 增删改查

    vue-table-ui 该工程提供的是 一个简单的 Vue + Element-UI 的表格,增删改查操作. 工程代码在最下面. 环境 jdk1.8 idea maven springboot 2. ...

  4. 用 Golang 从0到1实现一个高性能的 Worker Pool(一) - 每天5分钟玩转 GPT 编程系列(3)

    目录 1. 概述 2. 设计 2.1 让 GPT-4 给出功能点 2.2 自己总结需求,再给 GPT 派活 3. 实现 3.1 你先随意发挥 3.2 你得让 Worker 跑起来呀 3.3 你说说 P ...

  5. 并发编程-FutureTask解析

    1.FutureTask对象介绍 Future对象大家都不陌生,是JDK1.5提供的接口,是用来以阻塞的方式获取线程异步执行完的结果. 在Java中想要通过线程执行一个任务,离不开Runnable与C ...

  6. oracle 11g手工建库步骤(初学者)

    要建立的数据库ORACLE_SID=test1sys和system的密码为oracle1.建立相应的目录mkdir /u01/app/oracle/oradata/test1mkdir /u01/ap ...

  7. rman catalog 遇到的一个错误

    [oracle@source admin]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Thu Jun 22 09: ...

  8. python3使用ESL和sipp自动多轮压测FreeSWITCH

    环境:CentOS 7.6_x64   FreeSWITCH版本 :1.10.9   sipp版本:3.6.1   python版本:3.9.12 日常工作中,有时会遇到批量自动压测FreeSWITC ...

  9. Log4j2的Maven依赖及其配置文件

    Maven依赖 <!-- log4j 2依赖--> <dependency> <groupId>org.apache.logging.log4j</group ...

  10. CAP 7.2 版本发布通告

    前言 今天,我们很高兴宣布 CAP 发布 7.2 版本正式版,我们在这个版本中主要致力于 Dashboard 对 k8s 服务发现的支持. 从 7.1 版本以来,我们发布了4个小版本,在这些版本中我们 ...