通过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;
            }
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值