<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    咖啡伴侶

    呆在上海
    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. }  


    主站蜘蛛池模板: 亚洲国产成人片在线观看| 免费h黄肉动漫在线观看| 99亚洲精品高清一二区| 99在线观看精品免费99| 97亚洲熟妇自偷自拍另类图片| 久久午夜免费鲁丝片| 亚洲美女在线观看播放| 永久免费av无码网站韩国毛片| 亚洲国产成人资源在线软件| 99re热免费精品视频观看| 亚洲精品无码日韩国产不卡av| 超pen个人视频国产免费观看| 国产成人亚洲精品播放器下载 | 亚洲AV无码一区二区三区在线观看| 国产精品亚洲专区无码唯爱网| 又粗又大又长又爽免费视频| 一区二区三区AV高清免费波多| 亚洲中文字幕无码永久在线| 国产无遮挡裸体免费视频在线观看 | 午夜国产羞羞视频免费网站| 特a级免费高清黄色片| 亚洲国产精品无码专区在线观看| 亚洲精品视频免费看| 亚洲欧美日韩自偷自拍| 亚洲乱码精品久久久久..| **真实毛片免费观看| 国产精品亚洲一区二区在线观看| 亚洲精品乱码久久久久久蜜桃 | 热99re久久免费视精品频软件 | 亚洲AV无码AV吞精久久| 亚洲精品97久久中文字幕无码| 华人在线精品免费观看| 久久亚洲精品国产精品婷婷 | 美女隐私免费视频看| 亚洲精品无码午夜福利中文字幕| 亚洲成年人免费网站| 久久久亚洲精华液精华液精华液| 亚洲AV无码国产丝袜在线观看 | 亚洲av午夜福利精品一区人妖| 精品久久久久国产免费| 99在线视频免费观看|