Android本地服務

來源:互聯網
上載者:User
//服務和訪問者在同一進程,所以叫本地服務//訪問者:package cn.com.localquery;import cn.com.service.ILocalQuery;import cn.com.service.LocalQueryService;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;//訪問者(用戶端)總結://用戶端(此處為MainActivity)的核心目的:得到服務返回來的Binder對象,然後利用此Binder對象調用服務裡的相關方法//步驟://1 寫一個LocalQueryServiceConnection類實現了ServiceConnection介面//  即LocalQueryServiceConnection implements ServiceConnection//2 建立LocalQueryServiceConnection類的對象conn.然後利用Context啟用服務,與服務建立串連//  即 this.bindService(service, conn, this.BIND_AUTO_CREATE);//3 LocalQueryServiceConnection類的public void onServiceConnected(ComponentName name, IBinder service)//  方法為一個回調方法!!!!當用戶端與服務建立串連後,服務端的public IBinder onBind(Intent intent)()方法//  就會返回一個IBinder對象給客服端.此IBinder對象就是方法//  public void onServiceConnected(ComponentName name,IBinder service)中的service//4 利用伺服器返回的IBinder對象調用服務中的方法//注意://1 在綁定和解除綁定用的都是從conn對象,它的類實現了ServiceConnection介面//2 注意不但要重寫MainActivity的onCreate()裡與服務建立串連,而且還要重寫Activity的 onDestroy()方法!以便在此Activity退出時,關閉與服務的串連//3 在這個小應用中為什麼要定義一個介面呢?//  因為服務要返回給MainActivity一個Binder對象,MainActivity接收此對象。此對象是在服務裡內部類的對象。//  但是一般來說內部類是私人的。所以在MainActivity裡不可能new 一個內部類對象來接收此對象//  所以在此定義了介面--->用介面來接收Binder對象.這樣做而且很規範,將業務抽象成介面.public class MainActivity extends Activity {    TextView numberTextView;    TextView resultTextView;    Button button;      LocalQueryServiceConnection conn=new LocalQueryServiceConnection();    ILocalQuery  localBinder; //用於接收服務返回的Binder對象    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                numberTextView=(TextView) findViewById(R.id.number);        resultTextView=(TextView) findViewById(R.id.result);        button=(Button) findViewById(R.id.button);        button.setOnClickListener(new ButtonOnClickListener());        Intent service=new Intent(this,LocalQueryService.class);//利用Intent啟用服務        bindService(service, conn, this.BIND_AUTO_CREATE);//當Activity啟動的時候就啟動服務    }        private class ButtonOnClickListener implements OnClickListener{public void onClick(View v) {String number=numberTextView.getText().toString();String result=localBinder.queryByNumber(Integer.valueOf(number));resultTextView.setText(result);}       }      //重寫Activity的onDestroy()方法,以便在此Activity退出時,關閉與服務的串連protected void onDestroy() {unbindService(conn);super.onDestroy();}    //接收綁定的服務和解除服務    private final class LocalQueryServiceConnection implements ServiceConnection{public void onServiceConnected(ComponentName name, IBinder service) { localBinder=(ILocalQuery) service;}public void onServiceDisconnected(ComponentName name) {//Activity執行onDestroy()時,此方法調用localBinder=null;}       }   }//服務端:package cn.com.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;//本地服務總結://1 自訂服務類LocalQueryService繼承自Service//2 寫一個自訂的內部類LocalQueryBinder實現了業務介面,且繼承自Binder//3 重寫Service的public IBinder onBind(Intent intent)方法,返回一個IBinder對象.即這裡的LocalQueryBinder類對象public class LocalQueryService extends Service {private String[] names = new String[] { "小明", "小王", "小楊", "小李" };LocalQueryBinder localQueryBinder = new LocalQueryBinder();@Overridepublic IBinder onBind(Intent intent) {return localQueryBinder;}// queryByNumber就是服務裡的方法.一般來講將業務抽象為一個介面,然後去實現介面,比如此處。private final class LocalQueryBinder extends Binder implements ILocalQuery {// Binder實現了IBinder介面public String queryByNumber(int number) {return query(number);// 調用了服務內部的方法}}public String query(int i) {// 服務內部的方法if (i > 0 && i < 5) {return names[i - 1];}return "查詢錯誤,請再次輸入";}}//介面:package cn.com.service;public interface ILocalQuery {   public String queryByNumber(int number);}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.