Android:Service(二)——以綁定方式啓動Service

關於Service的線程問題

上一篇說了Service的基本使用:Android:Service(一)——Service的基本使用,其中有一點沒有提到的是:在service上運行的任務也是運行在主線程上的,所以不要以爲開啓了服務就可以在裏面盡情地做一些耗時任務。不然的話也會導致ui卡頓~可以做這麼一個實驗,在Activity上輸出當前線程的Id然後在Service上也輸出當前線程的id,就會發現他們的id是一致的,也就是說在service 上也是主線程在運行~因此若要在service上運行耗時任務記得先開一條子線程,然後在這子線程上運行。


關於綁定Service

用startService開啓的服務,一旦開啓之後,該service與activity就沒有關係了。也就是說,activity很難調用到service上的方法,很難通過用戶在activity上的操作去改變service的任務。同時,activity銷燬也與service無關。activity銷燬了之後service還在運行~
如果是通過綁定service啓動service那麼就不存在這些問題了。

接下來就說說如何綁定Service,
還是跟平常一樣新建一個Service,可以看到剛新建的Service裏面有一個onBind方法,在Activity進行綁定Service之後會把onBind所返回的對象返回到onServiceConnected,我們只需要在該回調裏接收所返回的對象就可以了。我們就是根據這個返回的對象來操作service裏面的方法。因此我們先新建一個內部類,把將要調用的方法都寫在這個內部類裏面,這裏我寫了個log的方法作爲示例。上面提到onBind會把一個對象返回到onServiceConnected裏,所以我們把剛定義好的內部類的對象放到onBind裏面,並且返回出去。我們可以看到onBind的返回值是IBinder,因此我們需要讓新建的內部類實現Ibinder接口,但是我們會發現Ibinder接口實在有太多方法需要實現,這個時候我們可以選擇繼承一個默認的實現類Binder。




package com.example.javy.bindservicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {


    @Override
    public IBinder onBind(Intent intent) {
        Log.e("bind","onBind");
      return new MyBind();
    }

    public class MyBind extends Binder{
        public void logMessage(String msg){
            Log.e("testlog",msg);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("serviceDes","onDestroy");
    }
}




好了service已經寫好了,接下來我們就要看看activity到底要怎樣寫才能綁定service~
首先先上佈局文件,佈局文件只是爲了方便測試使用~

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

  <Button
      android:text="綁定"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/mybtnbind"
      />
    <Button
        android:text="解綁"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/unbind"
        />
    <Button
        android:text="Log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/log"
        />
</LinearLayout>

然後看java代碼:


package com.example.javy.bindservicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnBind;
    private Button btnUnbind;
    private MyConn conn;
    private MyService.MyBind mybind;
    private Button btnLog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }
    private void initView(){
        btnBind=(Button)findViewById(R.id.mybtnbind);
        btnUnbind= (Button) findViewById(R.id.unbind);
        btnLog= (Button) findViewById(R.id.log);
        btnBind.setOnClickListener(this);
        btnUnbind.setOnClickListener(this);
        btnLog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(conn==null){
            conn=new MyConn();

        }
        if(v.equals(btnBind)){
            Intent intent =new Intent(this,MyService.class);
            bindService(intent, conn, BIND_AUTO_CREATE);

        }
        if(v.equals(btnUnbind)){
            unbindService(conn);
        }
        if(btnLog.equals(v)){
            mybind.logMessage("挖槽~還真的可以啊");

        }
    }

    private class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("bindLog","bind");
            mybind= (MyService.MyBind) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            
            Log.e("bindLog","unbind");
        }
    }
}

我們來看看綁定的操作:
        if(v.equals(btnBind)){
            Intent intent =new Intent(this,MyService.class);
            bindService(intent, conn, BIND_AUTO_CREATE);

        }

跟普通的啓動service一樣,也是新new一個intent出來,不同的是所調用的方法不是startService而是bindService。
bindService有三個參數,第一個是傳入intent,第二個傳入ServiceConnection,第三個傳入flag~上文的BIND_AUTO_CREATE的意思是,如果服務不存在則創建一個的意思,更多flag請自行了解,本文主要是講述如何通過綁定service的方式啓動service。我們再看看那個ServiceConnection,Activity定義了一個內部類MyConn繼承了ServiceConnection,其中有兩個回調方法,其中一個是與service建立起連接的時候回調的,另一個是與Service斷開連接的時候回調的~
再看該內部類的兩個回調方法,其中onServiceConnected傳進兩個參數,其中一個是IBinder,該參數正是在Service的onBind返回的值~經過強轉,用已經定義好的mybind接收。

最後使用mybind調用service裏的方法。。。


想要解綁service也非常簡單,只需調用unbindService,並傳入綁定時所傳入的ServiceConnection就可以了。



通過這種方式啓動的service,如果activity銷燬了,所綁定的service也隨之銷燬。
並且一旦綁定後,如果解綁該service那麼該service也會銷燬。


好吧~礙於筆者表達能力關係表達得不太清楚~因此筆者上傳一個demo給大家看看~~放心~免費的~~哈哈哈








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