Android提高第五篇之Service

Android提高第五篇之Service

上次介绍了Activity以及Intent的使用,这次就介绍Service,如果把Activity比喻为前台程序,那么Service就是后台程序,Service的整个生命周期都只会在后台执行。Service跟Activity一样也由Intent调用。在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加。

         Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。先贴出本文程序运行截图:

本文主要讲解Service的调用,以及其生命周期。

上图是startService之后再stopService的Service状态变化。

上图是bindService之后再unbindService的Service状态变化。

       startService与bindService都可以启动Service,那么它们之间有什么区别呢?它们两者的区别就是使Service的周期改变。由startService启动的Service必须要有stopService来结束Service,不调用stopService则会造成Activity结束了而Service还运行着。bindService启动的Service可以由unbindService来结束,也可以在Activity结束之后(onDestroy)自动结束。

上图是startService之后再Activity.finish()的Service状态变化,Service还在跑着。

上图是bindService之后再Activity.finish()的Service状态变化,Service最后自动unbindService。

main.xml代码:


  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.         android:orientation="vertical" android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent">
  4.         <button android:layout_width="wrap_content" 
  5.                 android:layout_height="wrap_content" android:id="@+id/btnStartMyService"
  6.                 android:text="StartMyService">
  7.         <button android:layout_width="wrap_content" 
  8.                 android:layout_height="wrap_content" android:id="@+id/btnStopMyService"
  9.                 android:text="StopMyService">
  10.         <button android:layout_width="wrap_content" 
  11.                 android:layout_height="wrap_content" android:id="@+id/btnBindMyService"
  12.                 android:text="BindMyService">
  13.         <button android:layout_width="wrap_content" 
  14.                 android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"
  15.                 android:text="UnbindMyService">
  16.         <button android:layout_width="wrap_content" 
  17.                 android:layout_height="wrap_content" android:id="@+id/btnExit"
  18.                 android:text="退出程序">

复制代码
testService.java的源码:

  1. package com.testService;

  2. import android.app.Activity;
  3. import android.app.Service;
  4. import android.content.ComponentName;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;

  12. public class testService extends Activity {
  13.     Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.         btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
  19.         btnStartMyService.setOnClickListener(new ClickEvent());
  20.         
  21.         btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
  22.         btnStopMyService.setOnClickListener(new ClickEvent());
  23.         
  24.         btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
  25.         btnBindMyService.setOnClickListener(new ClickEvent());
  26.         
  27.         btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
  28.         btnUnbindMyService.setOnClickListener(new ClickEvent()); 
  29.         
  30.         btnExit=(Button)this.findViewById(R.id.btnExit);
  31.         btnExit.setOnClickListener(new ClickEvent());
  32.     }
  33.     @Override
  34.     public void onDestroy()
  35.     {
  36.             super.onDestroy();
  37.             Log.e("Activity","onDestroy");
  38.     }
  39.     
  40.     private ServiceConnection _connection = new ServiceConnection() {  
  41.                 @Override
  42.                 public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  43.                         // TODO Auto-generated method stub
  44.                 }

  45.                 @Override
  46.                 public void onServiceDisconnected(ComponentName name) {
  47.                         // TODO Auto-generated method stub
  48.                 }  
  49.     };  
  50.     class ClickEvent implements View.OnClickListener{

  51.                 @Override
  52.                 public void onClick(View v) {
  53.                         Intent intent=new Intent(testService.this,MyService.class);
  54.                         if(v==btnStartMyService){
  55.                                 testService.this.startService(intent);
  56.                         }
  57.                         else if(v==btnStopMyService){
  58.                                 testService.this.stopService(intent);
  59.                         }
  60.                         else if(v==btnBindMyService){
  61.                                 testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
  62.                         }
  63.                         else if(v==btnUnbindMyService){
  64.                                 if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
  65.                                         testService.this.unbindService(_connection);
  66.                         }
  67.                         else if(v==btnExit)
  68.                         {
  69.                                 testService.this.finish();
  70.                         }
  71.                         
  72.                 }
  73.             
  74.     }
  75. }
复制代码
MyService.java的源码:

  1. package com.testService;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;

  6. public class MyService extends Service {
  7.         static public String ServiceState="";
  8.         @Override
  9.         public IBinder onBind(Intent arg0) {
  10.                 Log.e("Service", "onBind");
  11.                 ServiceState="onBind";
  12.                 return null;
  13.         }
  14.         @Override
  15.         public boolean onUnbind(Intent intent){
  16.                 super.onUnbind(intent);
  17.                 Log.e("Service", "onUnbind");
  18.                 ServiceState="onUnbind";
  19.                 return false;
  20.                 
  21.         }
  22.         @Override
  23.         public void onCreate(){
  24.                 super.onCreate();
  25.                 Log.e("Service", "onCreate");
  26.                 ServiceState="onCreate";
  27.         }
  28.         @Override
  29.         public void onDestroy(){
  30.                 super.onDestroy();
  31.                 Log.e("Service", "onDestroy");
  32.                 ServiceState="onDestroy";
  33.         }
  34.         @Override
  35.         public void onStart(Intent intent,int startid){
  36.                 super.onStart(intent, startid);
  37.                 Log.e("Service", "onStart");
  38.                 ServiceState="onStart";
  39.         }

  40. }
复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值