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 ...
随机推荐
- 性能_3 jmeter连接数据库jdbc(sql server举例)
一.下载第三方工具包驱动数据库 1. 因为JMeter本身没有提供链接数据库的功能,所以我们需要借助第三方的工具包来实现. (有这个jar包之后,jmeter可以发起jdbc请求,没有这个jar包, ...
- 龙芯电脑编译redis (loongarch)
1.获取源码 源码地址:https://redis.io/download/#redis-downloads 最新版本是7.2,这里用redis5测试,最后一个版本是5.0.14 wget https ...
- 【Linq】Value cannot be null. (Parameter 'row')
报错代码: 原因分析: Linq中左连接后,取值需要处理null值;如下图修改后恢复正常.
- 如何构建高效、可观的系统「GitHub 热点速览」
经典老项目 system-design 教你如何设计一个健壮的系统,新项目 noodle 教你如何提升教育效率,而后者甚至单日获得了 1,600 star,刚开源就获得了 6k+ 的 star. 除了 ...
- 【Python】数据可视化利器PyCharts在测试工作中的应用
PyCharts 简介 PyCharts 是一个基于 Python 的数据可视化库,它支持多种图表类型,如折线图.柱状图.饼图等.PyCharts 提供了简洁的 API,使得用户能够轻松地创建各种图表 ...
- adb如何做Android ui自动化(这一篇就够了)
一.简介 我们都知道在做Android ui自动化的时候用的是appium,环境搭建贼难受.如果我们在工作中遇到需要实现简单的自动化功能,可以直接使用adb来完成,无需去搭建繁琐的appium.ADB ...
- Int64针对32位架构是按照4字节还是8字节对齐?
作为构建.NET的标准,CLI Spec(ECMA-335)针对基元类型的对齐规则具有如下的描述.按照这个标准,我们是这么理解的:8字节的数据类型(int64.unsigned int64和float ...
- 想在golang里用好泛型还挺难的
golang的泛型已经出来了一年多了,从提案被接受开始我就在关注泛型了,如今不管是在生产环境还是开源项目里我都写了不少泛型代码,是时候全面得回顾下golang泛型的使用体验了. 先说说结论,好用是好用 ...
- Linux 命令:find/grep/sed/awk/du/df
目录 find grep sed awk du/df find # find 属于全部匹配,如输入abc,不能查到abcd # find 默认采用递归搜索 # 按时间 find . -atime -1 ...
- 网关冗余协议:FHRP、HSRP(思科)、VRRP、GLBP
参考链接: CHANNEL技术与网关冗余 VRRP和HSRP的区别