android 中service的简单事例
- 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的简单事例的更多相关文章
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android中Service的使用详解和注意点(LocalService)
Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...
- Android中Service的一个Demo例子
Android中Service的一个Demo例子 Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding. 本文,主要贴代码,不对Servic ...
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- Android中Service的使用
我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...
- Android中Service深入学习
概述 1.当用户在与当前应用程序不同的应用程序时,Service可以继续在后台运行. 2.Service可以让其他组件绑定,以便和它交互并进行进程间通信. 3.Service默认运行在创建它的应用程序 ...
- Android中Service与多个Activity通信
由于项目需要,我们有时候需要在service中处理耗时操作,然后将结果发送给activity以更新状态.通常情况下,我们只需要在一个service与一个activity之间通信,通常这种情况下,我们使 ...
- Android中Service概述
Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...
- android中的回调简单认识
首先说一下最抽象的形式--2个类,A类和B类.A类含有1个接口.1个接口变量.(可能含有)1个为接口变量赋值的方法以及1个会使用接口变量的"地方";B类实现A中的接口,(可能)含有 ...
- Android中Service和Activity之间的通信
启动Service并传递数据进去: Android中通过Intent来启动服务会传递一个Intent过去. 可以在Intent中通过putExtra()携带数据 Intent startIntent ...
随机推荐
- Kubernetes(k8s)访问控制:权限管理之RBAC鉴权
目录 一.系统环境 二.前言 三.Kubernetes访问控制 四.鉴权简介 五.配置客户端机器 六.设置k8s集群允许所有请求访问 七.设置k8s集群拒绝所有请求访问 八.RBAC授权 8.1 ro ...
- Go 语言 context 都能做什么?
原文链接: Go 语言 context 都能做什么? 很多 Go 项目的源码,在读的过程中会发现一个很常见的参数 ctx,而且基本都是作为函数的第一个参数. 为什么要这么写呢?这个参数到底有什么用呢? ...
- Git存储
Git还提供了一个贮藏的功能.如果你某个分支开发过程中,这个分支的内容是要在本月月底上线的,但是生产上已经出现了一个重大bug,需要你立马去修复.你在分支开发的内容已经开发一部分了,工作区有内容是不能 ...
- 图扑 AR 技术应用与管理:施工建造、机柜扫描、办公室导航解决方案
随着科技的不断革新和创新,越来越多的行业开始迎来数字化时代的变革.建筑行业作为人类历史上最重要的产业之一,在数字化转型方面同样也在不断推进.图扑软件结合 AR 技术的应用,为建筑行业带来了更加便捷高效 ...
- Java IO流 - 字节流的使用详细介绍
IO流的基本介绍: IO流的概述: i 表示intput,是数据从硬盘文件读入到内存的过程,称之输入,负责读. o 表示output,是内存程序的数据从内存到写出到硬盘文件的过程,称之输出,负责写. ...
- [HUBUCTF 2022 新生赛]simple_RE
[HUBUCTF 2022 新生赛]simple_RE 查壳,64位 找main函数,F5查看伪代码,简单分析一下 int __cdecl main(int argc, const char **ar ...
- 【转载】Linux虚拟化KVM-Qemu分析(三)之KVM源码(1)
原文信息: 作者:LoyenWang 出处:https://www.cnblogs.com/LoyenWang/ 公众号:LoyenWang 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作 ...
- C#中DataTable的一些使用(后续继续补充)
C#中DataTable的一些使用 新建一个DataTable DataTable table = new DataTable(); table.Columns.Add("姓名", ...
- 这可能是前端处理excel最好的工具了
大家好,我是程序视点的小二哥! 今天小二哥要分享的是一个纯前端实现读取和导出excel文件的工具库:ExcelJS ExcelJs 简介 功能十分简单: 读取,操作并写入电子表格数据和样式到 XLSX ...
- 26种source-map看花了眼?别急,理解这几个全弄懂
上一篇 webpack处理模块化源码 的文章中提到了 "source map",这一篇来详细说说. 有什么作用 source map 用于映射编译后的代码与源码,这样如果编译后的代 ...