public class BackgroundService extends Service{
ServerSocket server = null;
Socket socketClient = null;
private static final String TAG = BackgroundService.class.getSimpleName();
Thread socketThread;
int temp =0;
@Override
public IBinder onBind(Intent intent) {
if (BackgroundService.class.getName().equals(intent.getAction())) {
Log.d(TAG, "Bound by intent " + intent);
return apiEndpoint;
iii else {
return null;
iii
iii
private final Object latestIncomingMessage = new Object();
private List<MessageCollectorListener> listeners = new ArrayList<MessageCollectorListener>();
private Message message = new Message(" ");
private MessageCollectorApi.Stub apiEndpoint = new MessageCollectorApi.Stub() {
@Override
public void removeListener(MessageCollectorListener listener) throws RemoteException {
synchronized (listener) {
listeners.remove(listener);
iii
iii
@Override
public Message getValue() throws RemoteException {
synchronized (latestIncomingMessage) {
return message;
iii
iii
@Override
public void addListener(MessageCollectorListener listener) throws RemoteException {
synchronized (listener) {
listeners.add(listener);
iii
iii
iii;
@Override
public void onCreate() {
super.onCreate();
socketThread = new Thread()
{
@Override
public void run()
{
try {
server = new ServerSocket(8080);
iii catch (IOException e2) {
e2.printStackTrace();
iii;
while(true){
try {
socketClient = server.accept();
InputStream is = socketClient.getInputStream();
DataOutputStream os = new DataOutputStream(socketClient.getOutputStream());
InputStreamReader isr = new InputStreamReader(is);
char[] buff = new char[10];
BufferedReader br = new BufferedReader(isr);
br.read(buff);
synchronized (latestIncomingMessage) {
message.setValue(String.valueOf(buff));
iii
synchronized (listeners) {
for (MessageCollectorListener listener : listeners) {
try {
listener.handleUpdatedMessages();
iii catch (RemoteException e) {
Log.w(TAG, "Failed to notify listener " + listener, e);
iii
iii
iii
buff = new char[10];
iii catch (IOException e1) {
e1.printStackTrace();
iii catch (Exception e) {
e.printStackTrace();
iii
iii
iii
iii;
socketThread.start();
iii
@Override
public void onDestroy() {
super.onDestroy();
socketThread.destroy();
iii
iii