咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

Android提高第五篇之Service 转载

Posted on 2011-07-21 10:10 oathleo 阅读(195) 评论(0)  编辑  收藏 所属分类: Android

Android提高第五篇之Service

分类: Android提高 2010-11-08 11:48 5360人阅读 评论(11) 收藏 举报

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

        上次介绍了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. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Button android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:id="@+id/btnStartMyService"  
  7.         android:text="StartMyService"></Button>  
  8.     <Button android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" android:id="@+id/btnStopMyService"  
  10.         android:text="StopMyService"></Button>  
  11.     <Button android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content" android:id="@+id/btnBindMyService"  
  13.         android:text="BindMyService"></Button>  
  14.     <Button android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"  
  16.         android:text="UnbindMyService"></Button>  
  17.     <Button android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content" android:id="@+id/btnExit"  
  19.         android:text="退出程序"></Button>  
  20. </LinearLayout>  

testService.java的源码:

  1. package com.testService;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Service;  
  5. import android.content.ComponentName;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. public class testService extends Activity {  
  15.     Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);  
  21.         btnStartMyService.setOnClickListener(new ClickEvent());  
  22.           
  23.         btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);  
  24.         btnStopMyService.setOnClickListener(new ClickEvent());  
  25.           
  26.         btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);  
  27.         btnBindMyService.setOnClickListener(new ClickEvent());  
  28.           
  29.         btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);  
  30.         btnUnbindMyService.setOnClickListener(new ClickEvent());   
  31.           
  32.         btnExit=(Button)this.findViewById(R.id.btnExit);  
  33.         btnExit.setOnClickListener(new ClickEvent());  
  34.     }  
  35.     @Override  
  36.     public void onDestroy()  
  37.     {  
  38.         super.onDestroy();  
  39.         Log.e("Activity","onDestroy");  
  40.     }  
  41.       
  42.     private ServiceConnection _connection = new ServiceConnection() {    
  43.         @Override  
  44.         public void onServiceConnected(ComponentName arg0, IBinder arg1) {  
  45.             // TODO Auto-generated method stub  
  46.         }  
  47.   
  48.         @Override  
  49.         public void onServiceDisconnected(ComponentName name) {  
  50.             // TODO Auto-generated method stub  
  51.         }    
  52.     };    
  53.     class ClickEvent implements View.OnClickListener{  
  54.   
  55.         @Override  
  56.         public void onClick(View v) {  
  57.             Intent intent=new Intent(testService.this,MyService.class);  
  58.             if(v==btnStartMyService){  
  59.                 testService.this.startService(intent);  
  60.             }  
  61.             else if(v==btnStopMyService){  
  62.                 testService.this.stopService(intent);  
  63.             }  
  64.             else if(v==btnBindMyService){  
  65.                 testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);  
  66.             }  
  67.             else if(v==btnUnbindMyService){  
  68.                 if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑  
  69.                     testService.this.unbindService(_connection);  
  70.             }  
  71.             else if(v==btnExit)  
  72.             {  
  73.                 testService.this.finish();  
  74.             }  
  75.               
  76.         }  
  77.           
  78.     }  
  79. }  

MyService.java的源码:

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



只有注册用户登录后才能发表评论。


网站导航: