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 ...
随机推荐
- 轻松掌握Python+主流测试框架Requests接口自动化,快速转型自动化测试
轻松掌握Python+主流测试框架Requests接口自动化,快速转型自动化测试 最近几年,自动化测试已经成为了软件测试的主流趋势,而Python语言和Requests库作为主流测试框架,也成为了越来 ...
- 前端学习C语言 - 函数和关键字
函数和关键字 本篇主要介绍:自定义函数.宏函数.字符串处理函数和关键字. 自定义函数 基本用法 实现一个 add() 函数.请看示例: #include <stdio.h> // 自定义函 ...
- GO 集合 map 使用总结
转载请注明出处: Go语言的集合称为映射(map),它是一种无序的键值对(key-value)的集合,集合是通过键(key)来快速检索值(value)的,键(key)类似于索引,它指向值(value) ...
- 22.04.1 wine8.10 完美安装同花顺最新版THS_9.20.40_20230613
Linux luma 5.19.0-45-generic #46~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 7 15:06:04 UTC 20 x86_64 ...
- rocketmq-console基本使用
rocketmq-console基本使用 作用:rocketmq-console是rocketmq的一款可视化工具,提供了mq的使用详情等功能. 一.安装部署 下载rocketmq组件 rocketm ...
- CF1829H Don't Blame Me题解
题意: 给定一个长度为 \(n\) 的数组,选择它的一个子序列(不一定要连续的),问有多少种选法使得它们 AND 的值的二进制表示法中有 \(k\) 个 \(1\). 思路: 这个题就是一个简单的 D ...
- Prometheus-4:服务自动发现Service Discovery
自动发现 Prometheus的服务发现的几种类型: 基于文件的服务发现: 基于DNS的服务发现: 基于API的服务发现:Kubernetes.Consul.Azure...... Prometheu ...
- 前后端分离实现注册+登录(Vue3.0 + Django3.2)
博客地址:https://www.cnblogs.com/zylyehuo/ 一.使用 vite+webstorm 搭建 Vue 环境,构建前端 1.结构树 2.main.js import { cr ...
- 如何动态修改 spring aop 切面信息?让自动日志输出框架更好用
业务背景 很久以前开源了一款 auto-log 自动日志打印框架. 其中对于 spring 项目,默认实现了基于 aop 切面的日志输出. 但是发现一个问题,如果切面定义为全切范围过大,于是 v0.2 ...
- return true 与 return false的妙用——jQuery
var arr = [1, 3, 5,7,9]; jQuery.each(arr, function(key, value){ if(key === 2){ return true; } consol ...