咖啡伴侶

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

          Android提高第五篇之Service 轉載

          Posted on 2011-07-21 10:10 oathleo 閱讀(201) 評論(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. }  


          主站蜘蛛池模板: 枣阳市| 竹溪县| 贵南县| 牟定县| 乌拉特中旗| 左权县| 平塘县| 西乡县| 屯门区| 子洲县| 襄城县| 霍邱县| 乐清市| 瑞昌市| 隆回县| 绍兴市| 如东县| 油尖旺区| 长寿区| 涟水县| 揭西县| 建平县| 梁河县| 青田县| 罗定市| 什邡市| 曲松县| 连南| 嵊泗县| 常州市| 德庆县| 祥云县| 桐梓县| 南投县| 乳源| 姚安县| 商城县| 建德市| 龙海市| 靖宇县| 张北县|