Answer the question
In order to leave comments, you need to log in
How to properly organize the application (interactions between threads and UI)?
Good afternoon. I ask for help in organizing the application. The application should display the value of the tags received from the OPC-DA server, in real time in a simple list (tag name - current value). Now done through IntentService and BroadcastReceiver. The problem of passing values from the service to the ListView, now there are about 100 values..
By clicking on the button, the IntentService is called and data is received in it.. Below is the onHandleIntent code:
protected void onHandleIntent(Intent workIntent) {
// Gets data from the incoming Intent
String dataString = workIntent.getDataString();
try{
server.connect();
Group group = server.addGroup("root");
branches = new ArrayList<Branch>(server.getTreeBrowser().browse().getBranches().size());
getBranchTree(server.getTreeBrowser().browse());
tags = new ArrayList<Leaf>();
for (Branch b: branches){
for(Leaf l:b.getLeaves()){
tags.add(l);
}
}
Log.i(LOG_TAG,"TAGS CREATED/ WE CAN READ VALUES FROM SERVER");
subscribe();
}catch(Exception ex){
Log.e(LOG_TAG,ex.getMessage());
}
}
///////////////////////////////////////////////
void subscribe() {
if (access != null) {
try {
access.unbind();
} catch (Exception ex) {
Log.w(LOG_TAG, ex.getMessage(), ex);
} finally {
access = null;
}
}
try {
access = new SyncAccess(server, 1000);
for(Leaf l: tags){
access.addItem(l.getItemId(), new DataCallback() {
ItemObject ItemObj = new ItemObject();
public void changed(final Item item, final ItemState itemState) {
// посылаем промежуточные данные
Intent updateIntent = new Intent();
updateIntent.setAction("ACTION_UPDATE");
updateIntent.addCategory(Intent.CATEGORY_DEFAULT);
//JIVariant jVar = itemState.getValue();
try{
ItemObj.ItemId = "awdasd";//item.getId().toString();
ItemObj.ItemQuality = 192;//itemState.getQuality().intValue();
ItemObj.ItemTimeStamp = "adas";//itemState.getTimestamp().toString();
ItemObj.ItemValue = "value";//itemState.getValue().toString();
updateIntent.putExtra("ACTION_UPDATE_OUT", ItemObj);
}
catch(Exception ex){
Log.e(LOG_TAG,ex.getMessage());
}
sendBroadcast(updateIntent);
if (itemState.getQuality ()==192) {
// responseIntent.putExtra(EXTRA_KEY_OUT, extraOut);
System.out.println(String.format("Item: %s, Value: %s, Timestamp: %tc, Quality: %d", item.getId(), itemState.getValue(), itemState.getTimestamp(), itemState.getQuality()));
Log.i(LOG_TAG, itemState.getValue().toString());
}
}
});
}
access.bind();
} catch (Exception ex) {
Log.w(LOG_TAG, ex.getMessage(), ex);
}
}
public class UpdateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//int update = intent.getIntExtra("ACTION_UPDATE_OUT", 0);
ItemObject Item = (ItemObject)intent.getParcelableExtra("ACTION_UPDATE_OUT");
Tag t = new Tag(Item.ItemId,Item.ItemValue,Item.ItemQuality);
tags.add(t);
tagAdapter.notifyDataSetChanged();
}
}
Answer the question
In order to leave comments, you need to log in
How can such a transfer be organized?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question