Integration with using subscription to broadcastΒΆ
- Create your own broadcast receiver
private PersonsReceiver receiver = new PersonsReceiver();
private class PersonsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
parse(context, intent);
}
}
- Subscribe it to events
net.seemetrix.agent.PERSONS
that appear every 0.5 sec
IntentFilter filter = new IntentFilter();
filter.addAction("net.seemetrix.agent.PERSONS");
try {
registerReceiver(receiver, filter);
} catch (IllegalArgumentException e){
Log.e("REGISTER EXCEPTION", "Receiver already registered" + e);
}
- To process received data in a json format
{
"persons": [
{
"id": int,
"duration": int,
"gender": String,
"age": String
}
]
}
Getting an array with objects containing information about people standing in front of the camera:
Bundle bundle = intent.getExtras();
String jsonstring = bundle.getString("persons");
JSONObject obj = new JSONObject(jsonstring);
jsonarray = obj.getJSONArray("persons");
Each object contains
int id = current_person.getInt("id");
id
β face identificator (as soon as a person appears in front of the camera he gets an identificator - a number that will correspond to it all the while he is standing in front of the camera.)
String gender = current_person.getString("gender");
gender
β gender (a line, can take the values male, female, undefined)
int duration = current_person.getInt("duration");
duration
- a period during which a person is standing in front of the camera (ms)
String age = current_person.getString("age");
age
β age (line, can take the values kid, young, adult and senior)
Example of an application that displays information about people standing in front of the camera
package com.example.parseltestapplication;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
private PersonsReceiver receiver = new PersonsReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("net.seemetrix.agent.PERSONS");
try {
registerReceiver(receiver, filter);
} catch (IllegalArgumentException e){
Log.e("REGISTER EXCEPTION", "Receiver already registered" + e);
}
}
@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(receiver);
} catch (IllegalArgumentException e){
Log.e("UNREGISTER EXCEPTION", "Receiver not registered" + e);
}
}
private class PersonsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
doMyWork(context, intent);
}
private void doMyWork(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String parse_result = "";
if (bundle != null) {
JSONArray jsonarray;
String jsonstring = bundle.getString("persons");
try {
JSONObject obj = new JSONObject(jsonstring);
jsonarray = obj.getJSONArray("persons");
if (jsonarray != null) {
Log.d("PARSE", "persons count " + jsonarray.length());
for (int i = 0; i < jsonarray.length(); ++i) {
JSONObject current_person = jsonarray.getJSONObject(i);
int id = current_person.getInt("id");
String gender = current_person.getString("gender");
int duration = current_person.getInt("duration");
String age = current_person.getString("age");
parse_result +=
i + " , id " + id + ",
gender " + gender + ",
age " + age + ",
duration " + duration + "\n";
}
Log.d("PARSE", parse_result);
} else {
Log.d("PARSE", "Null array");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
((TextView)findViewById(R.id.info)).setText(parse_result);
}
}
}