Service 初階

Service 是安卓的四大組件之一,使用的時候需要在清單文件中進行註冊。

注意:
1. 在清單文件中註冊,一般只需引入name 屬性;
2. 服務不能自己運行,需要通過調用啓動方法,啓動服務。 一般爲活動Activity中啓動服務。

  1. 應用場景

    1.1 執行需要長時間運行的操作,這個操作不與用戶進行交互,如網絡下載、大文件I/O 、複雜計算等。
    1.2 兩個應用間動態交互一般通過Server來完成。
    應用內或應用間數據通信,Android每個應用程序都在自己的dalvik虛擬機中運行。
    一個應用是不允許訪問其他應用的內存信息的,爲此Android 引入了 Content Provider在不同的應用間共享數據,
    BroadcastReceiver 廣播信息給不同的應用程序,但 Content Provide 更多用於數據的共享,BroadcastReceiver
    廣播信息會被所有應用接收較耗費系統的資源,對於兩個應用間動態的進行交互還需要通過Service 來完成。

  2. Service 的實現方式:
    繼承Service 類

    public class MyServer extends Service

    重寫綁定方法

    public IBinder (Intent intent){ rerurn null;}

  3. 啓動 Service 有兩種方式

    第一種:通過startServer() 啓動Service

    生命週期:
    onCreate()- >onStart()(可多次調用)–服務運行->onDestroy()

    如果Service還沒有運行,則android先調用onCreate()
    然後調用onStart();

    如果Service已經運行,則只調用onStart(),
    所以一個Service的onStart方法可能會重複調用多次。

    開啓者調用stopService的時候直接onDestroy,
    如果是開啓者自己直接退出而沒有調用stopService的話,Service會一直在後臺運行。
    該Service的開啓者再啓動起來後可以通過stopService關閉Service。

    第二種:通過bindService() 啓動Service 該方式提供了Context,例如活動等組件調用Sercive內部方法的機制。

    生命週期:
    onCreate()- >onBind()(只一次,不可多次綁定) -> onUnbind->onDestroy()

    onBind方法將返回給客戶端一個IBind接口實例,IBind允許客戶端回調服務的方法,比如得到Service運行
    的狀態或其他操作。

  4. Service 常用的方法 ,可以重寫的方法

    //綁定的方法
    public IBinder onBind(Intent intent) {}
    //創建的時候調用
    public void onCreate() {super.onCreate();}
    //服務開啓調用的方法,已過時,現已使用onStartCommand 來處理數據。
    public void onStart(Intent intent, int startId) {super.onStart(intent, startId);}
    //創建後調用該方法 該方法會處理一些數據信息(參數爲一些數據)
    public int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId);}
    //服務銷燬的方法
    public void onDestroy() {super.onDestroy();}

    Context調用服務使用的方法:
    例如:活動中調用
    啓動服務:
    1. // 開啓服務
    startService(intent);
    Intent intent = new Intent(MainActivity.this, MyService.class);
    2.// 綁定服務
    bindService(intent, new MyConn(), BIND_AUTO_CREATE);// service intent,conn,ServiceConnection連接,flags,標示
    Intent intent = new Intent(MainActivity.this, MyService.class);

    class MyConn implements ServiceConnection {
    // onServiceConnected 當服務連接的時候
    //IBinder 服務返回的IBinder對象
    public void onServiceConnected(ComponentName name, IBinder service) {   }
    // onServiceDisconnected 當服務斷開的時候調用
    public void onServiceDisconnected(ComponentName name) { }
    }
    
    3.stopService(intent);
    4.unbindService(conn);
    
  5. 常見的應用情形: Service的信息交互

    //綁定者

    bindService(Intent service, ServiceConnection conn, int flags);//綁定服務

    //參數一:通過intent意圖,指定要綁定的服務
    //參數二:服務連接狀態的接口 //參數三:綁定server方式的表示

    實現ServiceConnection 接口的兩個方法:
    public void onServiceConnected(ComponentName name, IBinder service) {}
    //參數一:已連接的服務的具體組件名稱
    //參數二:onBind方法返回的Binder對象

    public void onServiceDisconnected(ComponentName name) {}
    //參數一:失去連接的服務的具體組件名稱

    //服務本身
    通過onBind方法的返回值向綁定者返回一個Binder對象(相當於中間人角色),從而去調用服務的方法

    5.1 Activity 中接收處理服務 Service 中的信息 通過 bindService() 活動來實現。

    主要代碼實現方式:

    方式一:安全性好的 ,使用接口回調: 該方法只提供接口中的方法,相當於通過接口進行過濾,篩選中符合的接口中的方法,較爲安全。

    在活動中:
    public class MainActivity extends Activity implements OnClickListener {
    private ServerConnect sc;
    private Intent intent;
    // 1.開啓服務的兩種模式 startServer 和BindServer
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 初始化控件
    Button bt_getserver = (Button) findViewById(R.id.bt_getserver);
    Button bt_bind = (Button) findViewById(R.id.bt_bind);
    // 初始化事件
    bt_getserver.setOnClickListener(this);
    bt_bind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bt_getserver:
    // 通過中間人調用
    sc.getOneNet();
    break;

    case R.id.bt_bind:
        // 獲取意圖對象,進行數據傳遞
        intent = new Intent(getApplicationContext(), MyService.class);
        // 2 綁定服務 tag 綁定自動創建
        bindService(intent, new MyConn(), BIND_AUTO_CREATE);
        break;
    default:
        break;
    }
    

    }

    class MyConn implements ServiceConnection {
    // 在服務連接的時候調用 service 爲服務器傳入的內容
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    sc = (ServerConnect) service;
    }

    // 在服務銷燬的時候調用
    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
    

    }
    }

    在服務中:

    public class MyService extends Service {
    private String tag=”MyService”;

    public IBinder onBind(Intent intent) { return new MyBinder();}

    private void getNet() { Toast.makeText(getApplicationContext(), “獲取網絡資源。。。”, 0).show();}

    class MyBinder extends Binder implements ServerConnect { public void getOneNet() {getNet();} }
    }

    接口中:
    public interface ServerConnect { void getOneNet();}

    在xml文件中:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章