源码

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. @Document元注解的使用

    @Documented注解标记的元素,Javadoc工具会将此注解标记元素的注解信息包含在javadoc中.默认,注解信息不会包含在Javadoc中.示例如下: 声明Book注解,并使用@Docume ...

  2. Prism进入视图时导航的三种方式

    Prism导航 新建视图UserControl及其ViewModel,被跳转的视图的VM需要实现INavigationAware 在App.xaml.cs中注册视图及其ViewModel // App ...

  3. 2023-06-23:redis中什么是缓存击穿?该如何解决?

    2023-06-23:redis中什么是缓存击穿?该如何解决? 答案2023-06-23: 缓存击穿是指一个缓存中的热点数据非常频繁地被大量并发请求访问,当该热点数据失效的瞬间,持续的大并发请求无法通 ...

  4. SQL Server 根据一个表数据修改另外一个表数据

    今天在写代码的时候发现一个有趣的问题,同时也暴露了之前写的代码有问题,还好之前没有出现重复的情况,及时发现了这个问题,及时改了回来,不然就GG了 下面先上代码,再给大家解说一下 CREATE TABL ...

  5. Jedis 参数异常引发服务雪崩案例分析

    作者:vivo 互联网服务器团队 - Wang Zhi Redis 作为互联网业务首选的远程缓存工具而被大面积使用,作为访问客户端的 Jedis 同样被大面积使用.本文主要分析 Redis3.x 版本 ...

  6. EF6连接oracle

    最近项目用到oracle,一直使用sql server,ef很方便连接mssql,但是连接oracle的方法网上很多文章,尝试很多次终于搞定,dbfirst或者codefirst也是可以的. 安装OD ...

  7. python 导出项目需要的库

    输入命令: pip freeze > requirements.txt 产生的文件内容如下: asgiref==3.4.0 Django==3.2.4 django-debug-toolbar= ...

  8. Ubuntu新建Django工程错误:ModuleNotFoundError: No module named 'distutils.core'

    ubuntu18.04 默认没有安装 pip ,需要安装 python3-pip,即可解决 1 sudo apt-get install python3-pip 在shell脚本中输入以下命令: 1 ...

  9. Python工具箱系列(三十九)

    使用zlib对数据进行压缩 现实世界中,大量存在着对数据压缩的需求.为此,python内置了zlib压缩库,可以方便的对任意对象进行压缩. 下述代码演示了对字符串进行压缩: import zlib # ...

  10. 达梦数据库: SQL查询报错《不是 GROUP BY 表达式解决方法》

    报错信息: ****: 第4 行附近出现错误: 不是 GROUP BY 表达式 修改办法: 达梦可以配置兼容参数,COMPATIBLE_MODE=4,静态参数,需要重启数据库后生效! sp_set_p ...