Android 中Service組件

來源:互聯網
上載者:User
  Serivce組件:

首先要繼承於Serivce,實現其生命週期中的方法,然後還需要在AndroidMainfest.xml中<service>聲明才能使用

可以調用startService來啟動一個服務,或者使用bindService來綁定一個已經存在的服務,還可以通過RPC(遠程進程調用)機制來實現不同進程之間Service的調用。

生命週期 :onBind,是一個必須實現的方法,返回一個綁定介面給service

onCreate:當Service第一次被建立時,由系統調用。

onStart(),當通過startService()方法啟動serivce時,該方法調用

onDestory當Serivce不再使用時,系統調用該方法

 

要使用一個Service需要在AndroidMainfest.xml檔案<service>聲明serivce,在<service>元素中添加<intent-filter>指定如何訪問該Service.

 

 

注意:調用startService 的生命週期是這樣的onCreate-----------à onStart

調用bindService 的生命週期是這樣的onCreate-----------à onBind

 

 

package com.king.android.controls;
  2 
  3 import android.app.Service;
  4 import android.content.Intent;
  5 import android.os.IBinder;
  6 import android.widget.Toast;
  7 
  8 /**
  9 
 10  * 描述:第一個服務執行個體示範
 11  * 作者:Andy.Liu
 12  * 時間: 2012-7-18  上午07:21:48
 13  **/
 14 public class FirstService extends Service {
 15     
 16     private static final String MY_SERVICE = "com.king.android.controls.my_service";//服務指定的動作
 17 
 18     @Override
 19     public IBinder onBind(Intent arg0) {
 20         
 21         Toast.makeText(FirstService.this, "onBind.............", Toast.LENGTH_LONG).show();
 22         return null;
 23     }
 24     @Override
 25     public void onCreate() {
 26         Toast.makeText(FirstService.this, "onCreate...........", Toast.LENGTH_LONG).show();
 27         super.onCreate();
 28     }
 29     
 30     @Override
 31     public void onStart(Intent intent, int startId) {
 32         Toast.makeText(FirstService.this, "onStart.............", Toast.LENGTH_LONG).show();
 33         super.onStart(intent, startId);
 34     }
 35     
 36     @Override
 37     public void onDestroy() {
 38         Toast.makeText(FirstService.this, "OnDestory............", Toast.LENGTH_LONG).show();
 39         super.onDestroy();
 40     }
 41 
 42 }
 43 
 44 
 45 
 46 
 47 
 48 package com.king.android.controls;
 49 
 50 import android.app.Activity;
 51 import android.app.Service;
 52 import android.content.ComponentName;
 53 import android.content.Intent;
 54 import android.content.ServiceConnection;
 55 import android.os.Bundle;
 56 import android.os.IBinder;
 57 import android.view.View;
 58 import android.view.View.OnClickListener;
 59 import android.widget.Button;
 60 import android.widget.Toast;
 61 
 62 import com.king.android.R;
 63 
 64 /**
 65 
 66  * 描述:示範Service的Activity
 67  * 作者:Andy.Liu
 68  * 時間: 2012-7-18  上午07:27:19
 69  **/
 70 public class ServiceActivity extends Activity implements OnClickListener{
 71     private static final String MY_SERVICE = "com.king.android.controls.my_service";//服務指定的動作
 72     @Override
 73     protected void onCreate(Bundle savedInstanceState) {
 74         super.onCreate(savedInstanceState);
 75         initView();
 76     }
 77     
 78     private void initView(){
 79         setContentView(R.layout.show_service_layout);
 80         Button btnSerivce = (Button) findViewById(R.id.btn_start);
 81         btnSerivce.setOnClickListener(this);
 82         btnSerivce = (Button) findViewById(R.id.btn_stop);
 83         btnSerivce.setOnClickListener(this);
 84         btnSerivce = (Button) findViewById(R.id.btn_bind);
 85         btnSerivce.setOnClickListener(this);
 86         btnSerivce = (Button) findViewById(R.id.btn_unBind);
 87         btnSerivce.setOnClickListener(this);
 88     }
 89 
 90     @Override
 91     public void onClick(View v) {
 92         Intent intent = null;
 93         switch(v.getId()){
 94         case R.id.btn_start:
 95             intent = new Intent();
 96             intent.setAction(MY_SERVICE);
 97             startService(intent);
 98             break;
 99         case R.id.btn_stop:
100             intent = new Intent();
101             intent.setAction(MY_SERVICE);
102             stopService(intent);
103             break;
104         case R.id.btn_bind:
105             intent = new Intent();
106             intent.setAction(MY_SERVICE);
107             bindService(intent, conn, Service.BIND_AUTO_CREATE);
108             break;
109         case R.id.btn_unBind:
110             intent = new Intent();
111             intent.setAction(MY_SERVICE);
112             unbindService(conn);
113             break;
114         }
115     }
116     
117     private ServiceConnection conn = new ServiceConnection() {
118         
119         @Override
120         public void onServiceDisconnected(ComponentName name) {
121             Toast.makeText(ServiceActivity.this, "串連成功", Toast.LENGTH_LONG).show();
122         }
123         
124         @Override
125         public void onServiceConnected(ComponentName name, IBinder service) {
126             Toast.makeText(ServiceActivity.this, "中斷連線", Toast.LENGTH_LONG).show();
127         }
128     };
129 
130 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.