Android--Serviceb的绑定与解绑

bindService()

作用:绑定Service服务 
手动调用bindService()后,自动调用内部方法:onCreate()、onBind()

unbindService()

作用:解绑Service服务 
手动调用unbindService()后,自动调用内部方法:onCreate()、onBind()、onDestory()

简单Demo

activity_main.xml




    
    

MainActivity.java

package com.example.dpl.demoservice;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

    private EditText editData;
    private MyService.Binder binder=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editData=findViewById(R.id.editData);
        findViewById(R.id.btnStartService).setOnClickListener(this);
        findViewById(R.id.btnStopService).setOnClickListener(this);
        findViewById(R.id.btnBind).setOnClickListener(this);
        findViewById(R.id.btnUnbind).setOnClickListener(this);
        findViewById(R.id.btnSyncData).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btnStartService:
                Intent intent=new Intent(this,MyService.class);
                //传递参数
                intent.putExtra("data",editData.getText().toString().trim());
                startService(intent);
                break;
            case R.id.btnStopService:
                stopService(new Intent(this,MyService.class));
                break;
            case R.id.btnBind:
                //绑定服务
                bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnUnbind:
                unbindService(this);
                break;
            case R.id.btnSyncData:

                if (binder!=null){
                    binder.setData(editData.getText().toString().trim());
                }

                break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

        binder= (MyService.Binder) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
}

MyService.java

package com.example.dpl.demoservice;

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

public class MyService extends Service {
    private boolean running=false;
    private String data="这是默认信息";

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {   //MainActivity中onServiceConnected()方法调用

        return new Binder();
    }

    public class Binder extends android.os.Binder{   //MyService的内部类
        public void setData(String data){
            MyService.this.data=data;
        }
    }

    //在MainActivity执行完startService后执行onCreate()和该方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        data=intent.getStringExtra("data");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {  //service在第一次创建时调用
        super.onCreate();
        running=true;
        new Thread(){    //执行后台线程
            @Override
            public void run() {
                super.run();

                while (running){    //循环打印
                    System.out.println(data);
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onDestroy() {   //关闭服务
        super.onDestroy();
        running=false;
    }
}

 

你可能感兴趣的:(Android,Native,基础)