- 浏览: 753131 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (139)
- 玩转Android (48)
- Android创意美工 (0)
- Android杂谈 (23)
- Android实例练习 (2)
- Android ROM研究 (5)
- Android NDK开发指南 (5)
- Android NDK (0)
- Android Tips (3)
- Windows Phone 7 (5)
- iPhone (0)
- HTML5学习室 (0)
- JAVA (9)
- SSH+ibatis (8)
- PHP (0)
- IT生活 (1)
- linux (2)
- C (4)
- C++ (1)
- web 前端 (1)
- 云计算 (0)
- 设计模式 (0)
- C# (2)
- 其他 (1)
- 数据结构 (5)
- Web开发 (10)
- 数据库 (3)
- 搜索引擎 (0)
- Go语言 (0)
最新评论
-
wi100sh:
多谢分享~
玩转Android---UI篇---ImageButton(带图标的按钮) -
zhanghaichang:
好文章的。
高性能web开发技术(一) -
yingang:
引用classes.dex.dex2jar.jar 拖入 j ...
Andorid杂谈---Apk文件的反编译 -
扶摇诺:
讲解的简明易懂,多谢啦!
玩转Android---UI篇---LinearLayout(线性布局) -
a13429921973:
更为详细的图文介绍,可参考这个http://blog.csdn ...
Android ROM研究---CyanogenMod源代码下载及编译
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中显示的信息
评论
+1
发表评论
-
玩转Android---2D图形及动画---动画分析(Tween详细分析)
2011-09-26 21:59 2579在Android系统中提供了两种动画实现方式:一种是Tween ... -
玩转Android---2D图形及动画---图片处理
2011-09-26 13:08 1721在Android中很多地方都使 ... -
玩转Android--UI篇--PreferenceActivity(开启wifi和音乐等)
2011-09-01 11:57 4783本测试主要是为了测试PreferenceActivity的使用 ... -
玩转Android---2D图形及动画---Gif动画
2011-08-05 09:36 2166由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的 ... -
玩转Android---2D图形及动画---Frame动画
2011-08-01 20:53 2027Frame动画其实就是逐帧动画,用法也比Tween动画简单,只 ... -
玩转Android---2D图形及动画---Tween动画
2011-07-31 22:53 2141Android平台提供了两类动画,分别是Tween动画,和Fr ... -
玩转Android---2D图形及动画---View类使用
2011-07-30 23:39 2756由于游戏界面是由大量美工资源图片构成的,所以,在设计游戏界面的 ... -
玩转Android--组件篇---Handler的使用
2011-07-30 14:01 5309public class Handler ... -
玩转Android---UI篇---ZoomControls放大缩小图片
2011-07-27 13:03 7210ZoomControls控件是一个可以缩放但控件,效果如下图 ... -
玩转Android---组件篇---TextSwitcher
2011-07-25 09:21 2150还记得有一次淘宝的电话面试的时候问了我一个关于Android的 ... -
玩转Android---组件篇---AnalogClock,DigitalClock
2011-07-10 19:28 2880首先要说的是,这两个控件并不是经常的使用,但是如果程序需要的话 ... -
玩转Android---组件篇---SeekBar,RatingBar,Chronometer
2011-07-10 19:12 2809今天补充三个组件的使用,避免日后忘记。它们分别是 SeekB ... -
玩转Android---组件篇---Handler的使用(2)
2011-05-28 17:15 2762对于Handler来说,它和与它调用它的Activity是出于 ... -
玩转Android---组件篇---Handler的使用(1)
2011-05-28 15:37 2113在android中,有很多功能是不能放在onCreate或者o ... -
玩转Android---事件监听篇---第2篇
2011-05-27 10:30 5199事件监听篇---第二篇 下面是各种常用控件的事件监听的 ... -
玩转Android---事件监听篇---第1篇
2011-05-26 21:29 9896事件就是用户与UI界面的交互时所触发的操作。比如点击某一个按钮 ... -
玩转Android---组件篇---数据存储之SQLite
2011-04-17 17:05 8437Android中通过SQLite数据库引擎来实现结构化数据存储 ... -
玩转Android---组件篇---数据存储之File
2011-04-17 11:07 1998我们可以将一些数据直接以文件的形式保存在设备中。例如,一些文本 ... -
玩转Andorid---组件篇---数据存储之preference
2011-04-14 21:58 2258程序是数据的输入、处 ... -
玩转Android---组件篇---Broadcast Receiver(广播接收器)
2011-04-14 19:24 29259Braodcast Receiver顾名思义 ...
相关推荐
玩转Android---组件篇---Handler的使用玩转Android---组件篇---Handler的使用玩转Android---组件篇---Handler的使用
Android Upload Service Easily upload files in the background with automatic Android Notification Center progress indication. Download the latest demo app APK which uses the library and try it ...
内容包含:android-support-v4_1.6.0_26_20120316.jar android-support-v4_1.6.0_26_20120623.jar android-support-v4_1.6.0_26_20120730.jar android-support-v4_1.6.0_26_20121109.jar android-support-v4_1.6.0_26...
•Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇-...
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
Android-Service组件应用-模拟下载
axure 的Android-L组件库,比较全
android-sdk\platforms\android-21
Android-ffmpeg-android-java.zip,使用HTTPS://GithuB.COM/Wrngs/FFMPEG-Android编译的FFMPEG二进制的Android Java库,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核...
android-sdk_r24.4.1-windows和android-ndk-r10e-windows-x86_64。文件太大只能传到百度云上
android-26 sdk下载 android-26 sdk下载 android-26 sdk下载 android-26 sdk下载
android-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windows
Android---Service Timer之执行周期任务
将android-17.zip解压到 Android\android-sdk\platforms 文件夹下。
http://s1.eoeandroid.com/sdk/4.1/android-sdk_r20-windows.zip(推荐) http://s1.eoeandroid.com/sdk/4.1/installer_r20-windows.exe Mac版: http://s1.eoeandroid.com/sdk/4.1/android-sdk_r20-macosx.zip ...
android linux交叉编译工具aarch64-linux-android-4.9版本!需要的可以下载!
最新版的android-support-v4.jar,1.39M左右,包含常用的类库,最新版的android-support-v4.jar,1.39M左右,包含常用的类库
Android-Service系列之断点续传下载