通过bindservice启动一个服务并从服务中持续获取消息

首先创建一个接口,用于服务中消息的回调

public interface IMessageCallBack {
    public void setMessage(String message);
}

再创建一个服务对象,每隔3秒向activity发送一条消息,直到发完10条

public class MyService extends Service {
    private static final String TAG = "MyService";
    MyBinder mMyBinder = new MyBinder();
    IMessageCallBack mIMessageCallBack;

    Timer mTimer = new Timer();

    TimerTask mTimerTask = new TimerTask() {
        int i = 0;
        @Override
        public void run() {
            if (i == 11) {
                mTimer.cancel();
                mTimerTask.cancel();
                stopSelf();
            }

                if (null != mIMessageCallBack) {
                    mIMessageCallBack.setMessage(i++ + "");
                }

        }
    };

    public class MyBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        mTimer.schedule(mTimerTask, 0, 1000);
        return mMyBinder;
    }

    public void setIMessageCallBack(IMessageCallBack iMessageCallBack) {
        this.mIMessageCallBack = iMessageCallBack;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

 

再创建一个自己的serviceconnection对象,用于抽象与服务的连接

public class MyServiceConnection implements ServiceConnection {
    private static final String TAG = "MyServiceConnection";
    
    MyService mMyService;
    IMessageCallBack mIMessageCallBack;
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d(TAG, "onServiceConnected: ");
        mMyService = ((MyService.MyBinder) service).getService();
        mMyService.setIMessageCallBack(mIMessageCallBack);
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(TAG, "onServiceDisconnected: ");

    //注意 在连接正常关闭的情况下是不会被调用的
    //该方法只在Service 被破坏了或者被杀死的时候调用. 
    //例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一, 
    //这个时候onServiceDisconnected() 就会被调用。

    }

    public void setIMessageCallBack(IMessageCallBack iMessageCallBack) {
        this.mIMessageCallBack = iMessageCallBack;
    }
}

在activity中实例化连接对象,通过连接对象与intent将activity与service进行绑定并设置回调方法

public class MainActivity extends AppCompatActivity implements IMessageCallBack {
    private static final String TAG = "MainActivity";
    TextView mTextView;
    MyServiceConnection mServiceConnection;
    public boolean isBindService = false;
    Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//--------------------------------------------------------
        mServiceConnection = new MyServiceConnection();
        mServiceConnection.setIMessageCallBack(MainActivity.this);
//-----------------------------------------------------------------------


        mTextView = findViewById(R.id.text);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                isBindService = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
            }
        });


        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBindService) {
                    unbindService(mServiceConnection);
                    isBindService = false;
                }
            }
        });

    }


    @Override
    public void setMessage(String message) {
        Log.d(TAG, "getMessage: " + message);
        if (message.equals("10")) {
            if (isBindService) {
                unbindService(mServiceConnection);
                isBindService = false;
            }
        }

    }
}

 

你可能感兴趣的:(通过bindservice启动一个服务并从服务中持续获取消息)