天天看點

android - BindService

關于安卓BindService 綁定問題 BindService 主要用于當使用者想要将活動activity與service進行綁定,當activity結束時(調用者)service也跟着一起結束了;而startService()隻能用來啟動服務,當服務啟動後,就與前台活動沒有了關系; 當我們要從service外部獲得service中的方法内容時,我們必須要Ibinder傳回的service對象才能操作;是以我們必須利用bindservice的方式去綁定服務擷取binder對象,利用binder的方式去擷取service的方法内容;

使用bindservice的具體過程如下: 1)建立意圖:Intent intent = new Intent(MainActivity.this,BindService.class); 2)建立bindservice對象:bindService(intent,conn,BIND_AUTO_CREATE); 3)建立内部類MyBinder、建立mybinder對象:

private  MyBinder myBinder = new MyBinder();
    class MyBinder extends Binder{
        public BindService getService(){
            return BindService.this;
        }
    }
           

4)onBind()傳回mybinder對象:

public IBinder onBind(Intent intent) {
        return myBinder;
    }
           

5)通過serviceconnection擷取Binder對象進一步來擷取service對象,最後調用service中的方法:

ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BindService.MyBinder binder = (BindService.MyBinder)service;
            BindService bindService = binder.getService();
            bindService.print();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
           

示例代碼: MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEvent();
    }
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BindService.MyBinder binder = (BindService.MyBinder)service;
            BindService bindService = binder.getService();
            bindService.print();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void initEvent() {
        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,BindService.class);
                bindService(intent,conn,BIND_AUTO_CREATE);
            }
        });
        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(conn);
            }
        });
    }
}
           

BindService

public class BindService extends Service {
    
    public void print(){
        Log.d(TAG, "print: BindService");
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "onStart");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
        
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind:");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.d(TAG, "onRebind");
        super.onRebind(intent);
    }

    private  MyBinder myBinder = new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }
    class MyBinder extends Binder{
        public BindService getService(){
            return BindService.this;
        }
    }
}