0
我在尋找一種快速方法將來自我的服務中的監聽器的數據傳送給一個活動。從服務器內部的監聽器接收數據
我試着用變量存儲當前值並在活動中讀取它,但這樣會變慢!
我從一項活動開始我的服務。我讀過,可以獨立於活動開始一項服務,也可以從活動中綁定到該服務。這可能是一種溶劑嗎?它會是什麼樣子?
這是我服務的一部分:
公共類的DataService延伸服務{
private VehicleManager mVehicleManager;
public int onStartCommand(Intent intent, int flags, int startId) {
context = getApplicationContext();
Intent intent1 = new Intent(this, VehicleManager.class);
bindService(intent1, mConnection, Context.BIND_AUTO_CREATE);
startThreadUpdatingDatabase();
return Service.START_NOT_STICKY;
}
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mVehicleManager = ((VehicleBinder) service).getService();
mVehicleManager.addListener(EngineSpeed.class, engineSpeedListener);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
EngineSpeed.Listener engineSpeedListener = new EngineSpeed.Listener() {
public void receive(Measurement measurement) {
final EngineSpeed data = (EngineSpeed) measurement;
new Thread(new Runnable() {
public void run() {
Double engineSpeedValue = data.getValue().doubleValue();//i want this value using in an activity!!
}
}).start();
}
};
}
使用「綁定本地服務」模式http://developer.android.com/guide/components/bound-services.html#Binding – pskink