import java.util.Date; import java.util.Random; import android.content.Context; import android.content.Intent; import android.util.Log; import ro.pub.cs.systems.eim.practicaltest01.general.Constants; public class ProcessingThread extends Thread { private Context context = null; private boolean isRunning = true; private Random random = new Random(); private double arithmeticMean; private double geometricMean; public ProcessingThread(Context context, int firstNumber, int secondNumber) { this.context = context; arithmeticMean = (firstNumber + secondNumber) / 2; geometricMean = Math.sqrt(firstNumber * secondNumber); } @Override public void run() { Log.d(Constants.PROCESSING_THREAD_TAG, "Thread has started! PID: " + Process.myPid() + " TID: " + Process.myTid()); while (isRunning) { sendMessage(); sleep(); } Log.d(Constants.PROCESSING_THREAD_TAG, "Thread has stopped!"); } private void sendMessage() { Intent intent = new Intent(); intent.setAction(Constants.actionTypes[random.nextInt(Constants.actionTypes.length)]); intent.putExtra(Constants.BROADCAST_RECEIVER_EXTRA, new Date(System.currentTimeMillis()) + " " + arithmeticMean + " " + geometricMean); context.sendBroadcast(intent); } private void sleep() { try { Thread.sleep(10000); } catch (InterruptedException interruptedException) { interruptedException.printStackTrace(); } } public void stopThread() { isRunning = false; } }