Android-Service
2015-07-24 20:27
453 查看
Service(服务)是能够在后台执行长时间运行操作并且不提供用户界面的应用程序组件。Service从本质上可以分为两类:
Started Service(启动)
应用程序组件(例如Activity)通过其他调用 startService()方法启动服务时,服务处于“started”状态。
这种service可以无限地运行下去,即使启动它的组件已经被销毁,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。
当服务停止时,系统将会将其销毁
Bound Service(绑定)
当其他组件调用bindService()方法时,服务被创建。接着客户端通过IBinder接口与服务通信。客户端通过unbindService()方法关闭连接。多个客户端能绑定到同一个服务并且当它们都解绑定时,系统销毁服务(服务不需要被停止)。
这两条路径并不是完全分开的。即是说,你可以和一个已经调用了 startService()而被开启的service进行绑定。比如,一个后台音乐service可能因调用 startService()方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。这种情况下,stopService()或 stopSelf()实际上并不能停止这个service,除非所有的客户都解除绑定。
服务的生命周期如下:
![](http://img.blog.csdn.net/20150308171105403)
Service类中的重要方法
onCreate():当Service第一次被创建时将立即被调用。如果服务已经运行,该方法不被调用。
onStartCommand(Intent intent,int flags,int startId):每次客户端调用startService(Intent)方法启动Service时都会回调该方法。一旦该方法执行,服务就启动并在后台无限期运行。
onDestroy():当该Service被关闭前将立即回调该方法。
onBinder(Intent intent):当其他组件调用bindService()方法与服务绑定时,系统调用该方法,返回一个IBinder对象,应用程序可以通过该对象和Service组件进行通信。如果系统不想允许绑定,则应该返回null。
onUnbind(Intent intent):当Service上绑定的所有客户端都断开连接时将回调该方法。
Service的声明
类似Activity和其他组件,开发人员必须在应用程序配置文件中声明全部的Service。在 < application > 标签中增加< Service >子标签。
例子,获取Service的成员变量i的值
创建EchoService类如下:
MainActivity类如下:
Started Service(启动)
应用程序组件(例如Activity)通过其他调用 startService()方法启动服务时,服务处于“started”状态。
这种service可以无限地运行下去,即使启动它的组件已经被销毁,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。
当服务停止时,系统将会将其销毁
Bound Service(绑定)
当其他组件调用bindService()方法时,服务被创建。接着客户端通过IBinder接口与服务通信。客户端通过unbindService()方法关闭连接。多个客户端能绑定到同一个服务并且当它们都解绑定时,系统销毁服务(服务不需要被停止)。
这两条路径并不是完全分开的。即是说,你可以和一个已经调用了 startService()而被开启的service进行绑定。比如,一个后台音乐service可能因调用 startService()方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。这种情况下,stopService()或 stopSelf()实际上并不能停止这个service,除非所有的客户都解除绑定。
服务的生命周期如下:
Service类中的重要方法
onCreate():当Service第一次被创建时将立即被调用。如果服务已经运行,该方法不被调用。
onStartCommand(Intent intent,int flags,int startId):每次客户端调用startService(Intent)方法启动Service时都会回调该方法。一旦该方法执行,服务就启动并在后台无限期运行。
onDestroy():当该Service被关闭前将立即回调该方法。
onBinder(Intent intent):当其他组件调用bindService()方法与服务绑定时,系统调用该方法,返回一个IBinder对象,应用程序可以通过该对象和Service组件进行通信。如果系统不想允许绑定,则应该返回null。
onUnbind(Intent intent):当Service上绑定的所有客户端都断开连接时将回调该方法。
Service的声明
类似Activity和其他组件,开发人员必须在应用程序配置文件中声明全部的Service。在 < application > 标签中增加< Service >子标签。
<service android:name="EchoService"></service>
例子,获取Service的成员变量i的值
创建EchoService类如下:
package com.example.usingservice; import java.util.Timer; import java.util.TimerTask; import android.R.integer; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class EchoService extends Service { private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder(); private Timer timer = null; private TimerTask task = null; private int i = 0; public class EchoServiceBinder extends Binder{//内部类 public EchoService getService() {//取得服务的实例 return EchoService.this; } } @Override public IBinder onBind(Intent arg0) { System.out.println("onBind"); return echoServiceBinder; } @Override public void onCreate() { System.out.println("onCreate"); startTimer();//启动定时器 super.onCreate(); } @Override public void onDestroy() { System.out.println("onDestroy"); stopTimer();//停止定时器 super.onDestroy(); } public void startTimer() {//启动timer if (timer == null) { timer = new Timer(); task = new TimerTask() { @Override public void run() { i++; System.err.println("i is "+i); } }; timer.schedule(task, 1000, 1000);//启动timer,延迟1s,周期1s } } public void stopTimer() {//停止timer if (timer != null) { task.cancel(); timer.cancel(); task = null; timer = null; } } public int getCurrentNum() {//取得i的值 return i; } }
MainActivity类如下:
package com.example.usingservice; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener, ServiceConnection { private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurNum; private Intent serviceIntent; private EchoService echoService = null;//服务 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this, EchoService.class); btnStartService = (Button) findViewById(R.id.startService); //启动服务button btnStopService = (Button) findViewById(R.id.stopService);//停止服务button btnBindService = (Button) findViewById(R.id.bindService); //绑定服务button btnUnbindService = (Button) findViewById(R.id.unbindServie);//un绑定服务button btnGetCurNum = (Button) findViewById(R.id.getCurrentNum);//获取当前service成员变量i数值的button btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurNum.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.startService: startService(serviceIntent);//启动服务 break; case R.id.stopService: stopService(serviceIntent);//停止服务 break; case R.id.bindService: bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);//绑定服务 break; case R.id.unbindServie: unbindService(this);//解绑服务 echoService = null; break; case R.id.getCurrentNum: if (echoService != null) { System.out.println("当前的i是:"+echoService.getCurrentNum()); } break; default: break; } } @Override public void onServiceConnected(ComponentName arg0, IBinder binder) {//成功绑定服务调用 System.out.println("onServiceConnected"); echoService = ((EchoService.EchoServiceBinder)binder).getService();//获取服务实例 } @Override public void onServiceDisconnected(ComponentName arg0) {//绑定服务Disconneted System.out.println("onServiceDisconnected"); } }
转载自:/content/7935812.html
相关文章推荐
- android--活用强大的自定义drawable功能
- Android中图片的放大。
- Android底层基础之NDK(一)
- Android Api Demos登顶之路(二)
- android 手机获取外置SD卡路径
- android的测试方法,
- android 不同手机外置SD卡路径不同
- Android Studio如何在真机中调试
- Android Ion
- Android 获取view在屏幕中的位置
- android getViewTreeObserver的使用
- universal-image-loader 使用思路
- 设置dialog的大小
- android代码的形式让button变成圆角 透明,如图
- Android 真机测试应用
- android:layout_weight的含义
- android 按钮按下效果(文字颜色和按钮同时变化)
- Android程序退出彻底关闭进程的方法
- Android FAT分区格式化
- Android 音频播放之SoundPool的使用和封装