Generate equation after a certain amount of time - android

I'm working on a school project in Android Studio and so far I've written a code which generates a random equation and then display this equation in a textview. Here is the code:
String[] operationSet = new String[]{"+", "-", "/", "*"};
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation = "";
for (int i = 0; i < numOfOperations; i++) {
equation += numbers.get(i);
equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView3);
TextEquation.setText(equation);
String stringResultOfEquation = String.valueOf(equation);
// Resultat der Rechung berechnen
double doubleAnswer = eval(stringResultOfEquation);
String stringAnswer = Double.toString(doubleAnswer);
TextView textAnswer = (TextView)findViewById(R.id.textView4);
textAnswer.setText(stringAnswer);
So far I've tried to use the TimerTask command:
TimerTask timerTaskWaiting = new TimerTask() {
#Override
public void run() {
}
};
Timer timerwaiting = new Timer();
timerwaiting.schedule(timerTaskWaiting, 5000);
I've put the "equation generater code" and put it into "public void run(){...}" but the app crashed when I tried it out.
My question now is, if there is a simple way which will generate the equation after a certain amount of time (for example 5 seconds) I mean, I want that the equation will be generated 5 seconds after the app is launched.
If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)
Thank you already in advance for your help!

This bit of code should suffice for you to understand what you need to do. obviously there are other ways you can achieve this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
generateAndDisplayEquation();
}
}, 5000);
}

Use RxJava library, it is very easy tool to organize periodic tasks.

Related

Android studio runnable, threads

I have two options to execute my program (I need to have Out_str as a result).
Option 1:
Out_str = "";
for (i = 1; i <= 20000; i++) {
Out_str = Out_str + "word";
}
Option2:
Runnable runnable1 = new Runnable() {
public void run() {
Out_str1 = "";
for (i1 = 1; i1 <= 10000; i1++) {
Out_str1 = Out_str1 + "word";
}
}
};
Runnable runnable2 = new Runnable() {
public void run() {
Out_str2 = "";
for (i2 = 1; i2 <= 10000; i2++) {
Out_str2 = Out_str2 + "word";
}
}
};
Thread thread1 = new Thread(runnable1);
thread1.start();
Thread thread2 = new Thread(runnable2);
thread2.start();
Why using threads does not make my program execute faster (in both cases the execution takes approx 12 sec with my laptop)? What shall I use in this case (to make it faster to obtain Out_str)? Will using services make help in this case?
The Thread allows you to run processes concurrently. You haven't gained any processing power though, with equal priority your threads would be run in a round-robin way I would guess.

Android, Use Timer to do a task at different times?

I searched other posts and I didn't find a case matching my problem, so please do not refer me to other posts.
I have a list of hundreds of different times starting like the below list:
timeList = {5, 13, 21, 40, ...}.
Suppose that they are saved in an arraylist. That means some task must be executed at 5 secs, 13 secs,... from a starting point . How can I use a Timer or any other way to achieve this?
Also I may have the timeList in a format like this if it helps:
timeList = {8:05, 8:13, 8:21, 8:40, ...}.
My current timer is like this:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
synchronized public void run() {
//do some task
}
}, startPoint, Period);
But this is obviously working at fixed rate!
By using Timer.schedule(TimerTask task, long delay), set all your timers and wait for them to execute
Assuming you are getting in MM:SS format (ex: 8:58):
ArrayList<String> your_arraylist = new ArrayList<String>();
//your list above, fill it with MM:SS
final ArrayList<Long> seconds = new ArrayList<Long>();
final ArrayList<Long> minutes = new ArrayList<Long>();
long first_minute;
for (String time : your_arraylist) {
minutes.add(Long.parseLong(time.split(":")[0]));
seconds.add(Long.parseLong(time.split(":")[1]));
}
first_minute = minutes.get(0);
for (int i = 0; i < seconds.size(); i++) {
long waiting_duration;
if (seconds.get(i) < Calendar.SECOND) {
waiting_duration = (60 - Calendar.SECOND + seconds.get(i)) * 1000;
}
else {
waiting_duration = (seconds.get(i) - Calendar.SECOND) * 1000;
}
waiting_duration += (minutes.get(i) - first_minute) * 60 * 1000;
Timer timer = new Timer();
TimerTask timer_task = new TimerTask() {
#Override
public void run() {
// do jobs
}
};
timer.schedule(timer_task, waiting_duration);
}
Don't use Timer use a Handler instead.
private int[] mTimeList = {5000, 2000, 7000, 4000};
private int mCount;
private int mDelay;
private Handler mHandler = new Handler();
private void initRepeatedTimer() {
mHandler.postDelayed(new Runnable() {
public void run() {
Log.e(TAG, "run: " + mCount);
mDelay = mTimeList[mCount];
if (mCount < mTimeList.length - 1)
mCount++;
else
mCount = 0;
mHandler.postDelayed(this, mDelay);
}
}, mDelay);
}
You wil notice that the log is printed as per delay in the list.

Calculating data rate per second but the result always the same

Iam calculating the cellular data rate per second by using Handler, the code is being executed every second and the overall traffic been calculated then it suppose to subtract the old traffic since boot from the current traffic since boot to get the current data rate per second.
The problem I'm facing that the current data rate value is not correct, it is always giving me the total overall traffic since boot. May be I did something wrong, I'm still beginner with android. The code below.
public class MainActivity extends AppCompatActivity {
private double RXOld;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
////////////////////////Code to be executed every second////////////////////////////////////////
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
double overallTraffic = TrafficStats.getMobileRxBytes();
double currentDataRate = overallTraffic - RXOld;
TextView view1 = null;
view1 = (TextView) findViewById(R.id.view1);
view1.setText("Current Data Rate per second= " + currentDataRate);
double RXOld = overallTraffic;
handler.postDelayed(this, 1000);
}
}, 1000 );
}
The new Code after rectification which shall give the current data rate per second
public class MainActivity extends AppCompatActivity {
final double [] RXOld = new double [1];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
////////////////////////Code to be executed every second////////////////////////////////////////
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
double overallTraffic = TrafficStats.getMobileRxBytes();
double currentDataRate = overallTraffic - RXOld [0];
TextView view1 = null;
view1 = (TextView) findViewById(R.id.view1);
view1.setText("Current Data Rate per second= " + currentDataRate);
RXOld [0] = overallTraffic;
handler.postDelayed(this, 1000);
}
}, 1000 );
First of all, consider using a Timer, and declare the TextView at the beginning. I think this code should work, but I didn't test it:
final TextView view1 = null;
view1 = (TextView) findViewById(R.id.view1);
final double[] oldT = new double[1];
Timer timer1 = new Timer();
TimerTask tt1 = new TimerTask() {
#Override
public void run() {
double overallTraffic = TrafficStats.getMobileRxBytes();
double trafficRate = overallTraffic - oldT[0];
oldT[0] = overallTraffic;
view1.setText("Current Data Rate per second= " + trafficRate);
}
};
timer1.scheduleAtFixedRate(tt1,1000,1000);
Note: if you use a new variable into the run() of the TimerTask, the variable MUST be declared final. You can easyly change its value by makeing the variable an array of 1 element ('int[] varName = new int[1]' instead of 'int varName'), and using varName[0] instead of varName

Appending letters onto a TextField after 100 milliseconds

I'm trying to append letters from a specific string onto a TextField. This is what I've tried so far. But no luck.
What happens here is it just waits for 100 milliseconds and then displays the string directly.
outConsole = (TextView) findViewById(R.id.outconsole);
int i, j;
String fin = "";
final String in = "HELLO!";
i = in.length;
try{
for(j = 0; j < i; j++){
Thread.sleep(100);
fin = fin + in.charAt(j);
outConsole.setText(fin);
}
}catch(Exception e){}
}
How do I achieve this?
I would like something like this but slower:
https://www.youtube.com/watch?v=Ff-eDOGLuYw
Try using Handlers. Something like this should work:
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
//update your ui
handler.postDelayed(this, 100);//this restarts the handler better to have some terminating condition
}
};
handler.postDelayed(runnable, 100);//starts the handler first time

How to set random images to ImageView's?

i am using 9 image view's i want set images to imageview randomly , when I click on refresh button, but I tried like this it's working for random allocation of images but it's repeating the same image in two (or) three imageview's at a time. where is the problem in my code..
final int[] imageViews = {
R.id.imgview11, R.id.imgview12, R.id.imgview13,
R.id.imgview21, R.id.imgview22, R.id.imgview23,
R.id.imgview31, R.id.imgview32, R.id.imgview33 };
final int[] images = {
R.drawable.i1, R.drawable.i2, R.drawable.i3,
R.drawable.i4, R.drawable.i5, R.drawable.i6,
R.drawable.i7, R.drawable.i8, R.drawable.empty };
final ImageButton shuffle = (ImageButton) findViewById(R.id.new_puzzle);
shuffle.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Random generator = new Random();
//int n = 9;
//n = generator.nextInt(n);
//Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[generator.nextInt(images.length - 1)]);
}
}
});
i don't want repeat, one image for one imageview only..
using the post of blessenm ,i wrote a similar code that you need. check if this helps you.
shuffle.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 9; i++)
{
while(true)
{
Integer next = rng.nextInt(9) ;
if (!generated.contains(next))
{
generated.add(next);
ImageView iv = (ImageView)findViewById(imageViews[i]);
iv.setImageResource(images[next]);
break;
}
}
}
}
});
Maybe not the perfect answer, but I would just shuffle the images list and the set the resulting image to the imageview.
This will avoid having to generate random numbers that will of course create duplicate (If you throw a dice 6 times, you won't have the numbers 1,2,3,4,5,6 in random order, you will get multiple time the same number.)
Please check everything including the 'i' as I am not in front of my computer.
List<int> list = Arrays.asList(images);
// Here we just simply used the shuffle method of Collections class
// to shuffle out defined array.
Collections.shuffle(list);
int i=0;
// Run the code again and again, then you'll see how simple we do shuffling
for (int picture: list) {
ImageView iv = (ImageView)findViewById(imageViews[i]);
iv.setImageResource(picture);
i++;
}
as an alternative, you may also want to shuffle your list with this code:
public class ShuffleArray {
public static void shuffleArray(int[] a) {
int n = a.length;
Random random = new Random();
random.nextInt();
for (int i = 0; i < n; i++) {
int change = i + random.nextInt(n - i);
swap(a, i, change);
}
}
private static void swap(int[] a, int i, int change) {
int helper = a[i];
a[i] = a[change];
a[change] = helper;
}
public static void main(String[] args) {
int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7 };
shuffleArray(a);
for (int i : a) {
System.out.println(i);
}
}
}
You might want to refer to this post. It shows a method to generate random numbers without duplicates
Creating random numbers with no duplicates

Categories

Resources