在this example(扩展Binder类部分)之后,可以在Activity中获取一个来自Service的字符串,只需按一个按钮。
但是如何在Activity中连续获取字符串更新?为了更好地解释,如果服务数据在ramdomly中变化,如何在Activity中获取相同的数据(字符串),而不按任何键?因为似乎获得的值是请求值服务的时间......
剪切活动
public void onButtonClick(View v) {
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
int num = mService.getRandomNumber();
Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
}
}
服务片段
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}