- 浏览: 868778 次
- 性别:
- 来自: 北京
最新评论
-
luna_2006:
...
【Java】枚举类型Enum介绍和方法详解 -
沙漠孤影:
...
【Android】Android Market 链接的生成与分享 -
sjl_008:
有错误,inSampleSize 为2时,图片大小不是原来的2 ...
【Android】如何解决bitmap 内存溢出out of memory的问题 -
huanglei_jay:
【Java】Collections.EMPTY_LIST和Collections.emptyList()简单使用心得 -
wuxuewujiang:
很详细!很有用的整理。
【Objective-C】NSDate详解及获取当前时间等常用操作
Service是Andorid系统提供的四大组件之一,它的地位和Activity是并列的,只是使用的频率没有Activity高。Service就是运行于后台的一种服务程序,一般很少和用户交互,因此没有可视化界面。
定义一个Service类只要继承Service类即可,实现其生命周期中的方法就可以了,另外,一个定义好的Service组件必须要在AndoridManifest.xml文件中注册才能够使用。
Service有自己的生命周期,可以调用startService()启动一个Service或者使用bindService()来绑定一个service,还可以通过RPC(远程进程调用)机制来实现不同进程间Service的调用。
Service中定义了一系列和自身生命周期相关的方法:
onBind(Intent intent):是必须实现的一个方法,返回一个绑定的接口给Service。
onCreate():当Service第一次被创建时,由系统调用。
onStart(Intent intent,int startId):当通过startService()方法启动Service时,该方法被调用。
onDestroy():当Service不再使用,系统调用该方法。
实例如下:
MyService.java
- /*
- * @author hualang
- */
- package org.hualang.service;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.widget.Toast;
- public class MyService extends Service {
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- Log.i("SERVICE", "onBind.................");
- Toast.makeText(MyService.this, "onBind.................", Toast.LENGTH_LONG).show();
- return null;
- }
- public void onCreate()
- {
- Log.i("SERVICE", "onCreate................");
- Toast.makeText(MyService.this, "onCreate................", Toast.LENGTH_LONG).show();
- }
- public void onStart(Intent intent,int startId)
- {
- Log.i("SERVICE", "onStart.................");
- Toast.makeText(MyService.this, "onStart.................", Toast.LENGTH_LONG).show();
- }
- public void onDestroy()
- {
- Log.i("SERVICE", "onDestroy.................");
- Toast.makeText(MyService.this, "onDestroy.................", Toast.LENGTH_LONG).show();
- }
- }
ServiceTest.java
- /*
- * @author hualang
- */
- package org.hualang.service;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class ServiceTest extends Activity {
- /** Called when the activity is first created. */
- private Button startService,stopService,bindService,unbindService;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- startService=(Button)findViewById(R.id.startButton);
- stopService=(Button)findViewById(R.id.stopButton);
- bindService=(Button)findViewById(R.id.bindButton);
- unbindService=(Button)findViewById(R.id.unbindButton);
- startService.setOnClickListener(startListener);
- stopService.setOnClickListener(stopListener);
- bindService.setOnClickListener(bindListener);
- unbindService.setOnClickListener(unbindListener);
- }
- private OnClickListener startListener=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setAction("org.hualang.service.action.MYSERVICE");
- startService(intent);
- }
- };
- private OnClickListener stopListener=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setAction("org.hualang.service.action.MYSERVICE");
- stopService(intent);
- }
- };
- private ServiceConnection conn=new ServiceConnection()
- {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- Log.i("SERVICE", "connection success");
- Toast.makeText(ServiceTest.this, "connection success", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- Log.i("SERVICE", "connection success");
- Toast.makeText(ServiceTest.this, "connection failure", Toast.LENGTH_LONG).show();
- }
- };
- private OnClickListener bindListener=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setAction("org.hualang.service.action.MYSERVICE");
- bindService(intent,conn,Service.BIND_AUTO_CREATE);
- }
- };
- private OnClickListener unbindListener=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setAction("org.hualang.service.action.MYSERVICE");
- unbindService(conn);
- }
- };
- }
在AndroidManifest.xml文件中注册Service如下
- <service android:name="MyService">
- <intent-filter>
- <action android:name="org.hualang.service.action.MYSERVICE"/>
- </intent-filter>
- </service>
main.xml布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="启动Service"
- android:id="@+id/startButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="停止Service"
- android:id="@+id/stopButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="绑定Service"
- android:id="@+id/bindButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="解除绑定unbindService"
- android:id="@+id/unbindButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
不解释,看运行结果就会明白
(1)当点击“启动Servcie”会先弹出"onCreate",然后弹出"onStart"的Toast,在LogCat中会看到先显示
的也是
onCreate
onStart
(2)点击“停止Service”按钮,会弹出onDestroy的Toast
LogCat中也会显示
onDestroy()
(3)点击"绑定Service"按钮,会弹出onCreate和onBind的Toast
LogCat中显示
onCreate
onBind
(4)点击“解除绑定Service”按钮,会弹出onDestroy的Toast,LogCat中也如此
下面是LogCat中显示的信息
发表评论
-
【Android】监听SDCard安装和卸载的代码片段(测试通过)
2012-03-21 18:39 2174// 监听类 private final Bro ... -
【Android】实现XML解析的几种技术
2012-03-16 16:52 3326本文介绍在Android平台中实现对XML的三种 ... -
【Android】AlarmManager简介
2012-03-15 20:15 1984AlarmManager 包含的主要方法: ... -
【Android】AlarmManager深入学习
2012-03-15 20:13 1974对应AlarmManage有一个AlarmManager ... -
【Android】this和this.getApplictionContext的区别 (转载)
2012-03-08 15:30 2813在android中常常会遇到与context有关的内容, ... -
【Android】在QQ通讯录,360之前拦截短信(intent-filter的优先级)
2012-06-26 11:28 3356原文:http://www.bangchui.org/sim ... -
【Android】xml文件里面出现unbound prefix的问题
2012-02-06 15:25 39254相信有很多菜鸟在编写xml文件的时候,都会遇 ... -
【Android】模拟器启动不了 emulator: ERROR: unknown virtual device name
2012-02-06 15:24 2999很多人在配置android环境的时候卡了壳,一切准备工作 ... -
【Android】Intent 和 Intent Filter 官方文档读后总结
2012-02-03 17:46 2244Intent 和 Intent Filter 1 ... -
【Android】Intent 和 Intent Filter
2012-02-03 17:42 1403转自:http://www.cnblogs.com/Andro ... -
【Android】常量DEFAULT_KEYS_SHORTCUT 功能的验证 及其 源码实现分析
2012-02-03 17:15 2353转自:http://blog.csdn.net/silence ... -
【Android】Android 手机杀进程并不省电,效果只能适得其反
2011-11-29 11:21 4904大家可能是被windows毒 ... -
【Android】BroadcastReceiver简介和用法实例,两种注册方式 (二)
2011-11-24 10:52 9928一.BroadcastReceiver简介 B ... -
【Android】BroadCastReceiver 简介
2011-11-24 10:50 3719BroadCastReceiver 简介 Bro ... -
【Android】BroadcastReceiver发送intent给service或者activity
2011-11-24 10:46 6939BroadcastReceiver:对象的生命周期不超过5s, ... -
【Android】理解Service和BroadcastReceiver(转载)
2011-11-24 10:45 2396Service与Activity类似,也需要在Manifest ... -
【Android】监听文件和目录的创建删除移动等事件
2011-11-16 10:36 2699出于安全上的需要,我们经常需要监听sd卡或者其他路径上的 ... -
【Android】如何在Activity中启动另一应用程序的方法,无需得到类名
2011-11-16 10:33 3084在网上搜索了一会相关的实现代码,发现所有的文章都说是需要 ... -
【Android】loadData与loadDataWithBaseURL的区别
2011-11-10 13:54 34115在写WebView时,感觉LoadUrl太浪费流量,而且 ... -
【Android】检测手机摇晃
2011-11-10 11:10 4392做游戏的时候,一般都要使用到多种多样的传感器,以实现比较丰 ...
相关推荐
Android 四大组件之Service的Demo
这是一个介绍android四大组件中Service的ppt文档,里面阐述了该组件的基本应用,非常有价值。
service的启动,停止,绑定,取消绑定,以及IntentService的生命周期走向测试和IntentService与访问者信息通信过程,不清楚之处请参阅我的博客《Android开发四大组件之Service总结》
Android四大组件——Service后台服务、前台服务、IntentService、跨进程服务、无障碍服务、系统服务
Android四大组件简述demo用例。
android四大组件详细介绍,activity,service等从基础到应用开发,熟练的掌握android 组件的应用。
android service 四大组件 完整代码 非常好用 非常经典 完整的代码.
文档主要是讲Android开发的四大组件详解,包含 Activity详解 ,Service详解,Broadcast Receiver详解,Content Provider详解 外加一个重要组件 intent的详解。 文章内容来自网络,本文是一个总结性文章。
Android四大组件之ServiceService的生命周期被启动(startService)的服务的生命周期:被绑定(bindService)的服务的生命周期:被启动又被绑定的服务的生命周期:实例布局创建Service类启动ServiceDemo下载 android开发...
安卓四大组件 Service 服务
博文android基础知识05:四大组件之service 02:远程调用中关于远程调用service的实例程序
根据博客内容,写的一个android 服务实例程序
主要介绍了Android四大组件之Service(服务)的用法,结合实例形式详细分析了Service的基本概念,类型,用法与相关注意事项,需要的朋友可以参考下
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器。
android service的实例程序,用于博文http://blog.csdn.net/xianming01/article/details/7341877
Android四大基本组件介绍与生命周期Android四大基本组件分别是Activity,Service服务,ContentProvider内容提供者,BroadcastReceiver广播接收器。 Android四大基本组件介绍与生命周期 Android四大基本组件分别是...
简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver
Android四大基本组件介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器......
今天小编就为大家分享一篇关于Android四大组件之Service详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧