In my android program, I have 3 functions that will be continuously repeated until the user exits the app. Currently, it is able to update a value(position) every second.
I was wondering if having lots of if else condition in one of the function will slow down the performance of my app i.e. it might take way longer than a second for the updates to kick in.
Note that 2 of the if else function will execute only once. All the if-else function have very little code in it. Below is the code
public void positionUpdated(Coordinate userPosition, int accuracy) {
// GETS EXECUTED ONCE //
if(nameList.size() == 0) {nameList = indoorsFragment.getZones();}
if(end == null) {
for(int i=0 ; i < nameList.size() ; i++) if(nameList.get(i).equals(name)) end = nameList.get(i).getZonePoints().get(0);
}
// GETS EXECUTED ONCE //
if(Math.abs(userPosition.x - end.x) >500) {Toast.makeText(this,"WALK " + Math.abs(end.x - userPosition.x)/1000 + " Meters" , Toast.LENGTH_SHORT).show();return;}
if(turn) {
if ((userPosition.y - end.y) < 0) {Toast.makeText(this, "STOP AND TURN RIGHT", Toast.LENGTH_LONG).show();turn=false;return;}
if ((userPosition.y - end.y) > 0) {Toast.makeText(this, "STOP AND TURN LEFT" , Toast.LENGTH_LONG).show();turn=false;return;}
}
if((Math.abs(userPosition.y - end.y) < 500)) Toast.makeText(this, "WALK", Toast.LENGTH_LONG).show();
}
Hope to receive some feedback regarding this , thanks
Haziq
The number of statements doesn't really matter, in fact they're executed in fractions of nanoseconds. what matters is if you have many nested loops or recursions.
If you don't want much work to be done in the foreground thread, use an AsyncTask or Runnable then update the UI in the main thread.
Related
I am making an app using ML KIT. I am currently implementing counting while doing squats. The problem is that it counts when it goes down to a certain angle, but because it is a real-time tracking, it counts multiple times rather than once. In other words, practically when I do one squat, the counting is much more than that.
So I tried solving the problem using a timer handler. But I failed. Here is my code What should I do?
//going down
if((rightKneeAngle<=90 && leftKneeAngle<=90) || (rightHipAngle<=135 && leftHipAngle<=135)){
//Timer setting
mTimer.schedule(new CustomTimer(), 2000);
Log.d("PoseGraphic.class", "goingdown"+"rightKneeAngle : " + rightKneeAngle+ " leftKneeAngle : " + leftKneeAngle);
Log.d("PoseGraphic.class","leftHip"+leftHipAngle+"RighHip"+rightHipAngle);
Log.d("PoseGraphic.class", "cnt: " + cnt);
//going up
if((rightKneeAngle>170 && leftKneeAngle>90)|| (rightHipAngle>135 && leftHipAngle>135)){
Log.d("PoseGraphic.class", "going up"+"rightKneeAngle : " + rightKneeAngle+ " leftKneeAngle : " + leftKneeAngle);
Log.d("PoseGraphic.class","leftHip"+leftHipAngle+"RightHip"+rightHipAngle);
if(cnt==12) {
canvas.drawText("complete!", x, y, textPaint);
//ExerciseCount.cnt = 0;
cnt=0;
}
}
}
// TIMER handler class
class CustomTimer extends TimerTask
#Override
public void run() {
cnt++;
}
I cannot fully understand how you do it without seeing more code.
An suggestion would be:
Instead of +1 to the count when the pose is in squat_down state, record the entering of that state with a boolean and +1 to the count only once the user exit squat_down and enter squat_up state. Then reset the state boolean.
By doing this, you will only +1 when user finish an entire cycle from squat_down to up.
Actually, ML Kit just launched an example for squat and pushup classification and rep-counting. Here is the app you can try. If you want to classify some other poses, here is some guide and a Colab.
In my doinBackground() function of Asynctask I am inserting heavy data in my database. At the same time I want to scroll list on Activity. But when doinBackground execute, it freeze or hang my list. At that time I am not able to scroll the list.I struct on this fromlast 2 days. I have tried a lot for this, but didn't get any clear description and reason. So, plzz if there is any IDEA that how to resolve it, will be very appreciable. Thanks in advance.
int i =0;
for(int j = i; j
for(int k = j; k<Integer.parseInt(listchscheduleArraylength.get(j));k++){
if(databasehelper.insert_into_table2(list_ch_autoid.get(j), list_sch_prg_id.get(k), list_sch_prg_title.get(k)
, list_sch_prg_gnre.get(k), list_sch_prg_sch_id.get(k), list_sch_prg_strt_time.get(k),
list_sch_prg_end_time.get(k), list_sch_prg_sch_date.get(k), list_sch_prg_sch_desc.get(k))){
Log.v("TABLE 2 status", "inserted");
}else {
Log.v("TABLE 2 status", " not inserted");
}
}// End of inner loop
} // End of outer loop
I need to insert a lot of data into a SQLite database. I use transactions for it. There is unfortunatly no chance I can fill the tables without these transactions. My code works like this:
database.transaction(function(transaction) {
for(var i = 0; i < 300; i++) {
sql = "INSERT INTO test (fielda, fieldb, fieldc) VALUES (111, 1, 23.53)";
transaction.executeSql(sql, function(result) {
});
}
}, function() {
database.transaction(function(transaction) {
for(var i = 0; i < 300; i++) {
sql = "INSERT INTO test (fielda, fieldb, fieldc) VALUES (111, 1, 23.53)";
transaction.executeSql(sql, function(result) {
});
}
}, function() {
.................and so on..............
alert("done");
}, function(error) {
alert("error");
});
}, function(error) {
alert("error");
});
It works fast and easy on my iPad 2 but crashes just as fast on my Galaxy Tab 3. It seems that the number of successive transactions doesn't matter but the number of inserts they have to perform altogether. Always around the 900th entry it crashes. That means it crashes in the 4th transaction with loops of i < 300 but needs 10 transactions with loops of i < 100 to crash.
I even tried calling 3 transactions with one button click, then waited for over ten minutes and calling the 4th transactions by clicking on another button. It still crashes.
There are happening other strange things as well:
When I call "DELETE FROM test" at some point before calling the transactions the app throws the error message "Cannot perform this operation because there is no current transaction." just before it crashes then during the transaction. But only then.
I already played around with every PRAGMA there is, like page_size, journal_mode, etc. It doesn't change a thing.
I am currently building a game in AndEngine, but my collision detection seems a bit off. It works.. most of the time, but seems like when there is a collision with one object, it wont do the other. It's hard to explain and it's very unpredictable.
for (int i = 0; i < rManager.carArray.length; i++)
{
if (rManager.getInstance().snowArray[0].getSnowSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
{
Log.e("SNOW", "snow 0 collided with " + rManager.getInstance().carArray[i].ToString());
rManager.getInstance().carArray[i].setCarSpeed(0.1f);
break;
}
if (rManager.getInstance().iceArray[0].getIceSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
{
Log.e("ICE", "ice 0 collided with " + rManager.getInstance().carArray[i].ToString());
rManager.getInstance().carArray[i].setCarSpeed(1f);
break;
}
else
{
rManager.getInstance().carArray[i].setCarSpeed(0.5f);
}
}
Is there anything wrong with my code? Currently, both enemy arrays only have 1 element. That is why I am only checking 0.
It is because of the "break" keyword. When that is called it "breaks out" of the surrounding for loop. So if the first if statement detects a collision, it will not ever call the second if statement.
I am using a Pool to manage Bullets in my game. The only problem is when a Bullet is obtained from the pool having just been recycled because it was involved in a collision, although it's Body's location is reset using Body.setTransform() when it is initialized, the Bullet's Sprite's location (which is used to detect collisions using Sprite.collidesWith(otherSprite)) is not reset quick enough (as it's updated in a Physics thread). This means the newly created bullet causes a collision the instant it is created, resulting in a single bullet causing more than one collision.
I tried calling Bullet.sprite.setPosition(0,0) when it is initialized, but this clearly interferes, as Bullets fail to be displayed at all with that line of code in place. What should I do to prevent this problem?
Bullet Creation:
bullets[bulletCounter] = bulletPool.obtainPoolItem();
bullets[bulletCounter].getBody().setTransform(shipBody.getTransform().getPosition(),0);
bullets[bulletCounter].getBody().setLinearVelocity(shipBody.getLinearVelocity());
bullets[bulletCounter].activate();
Collision Detection:
for(int i = 0; i < BULLET_MAX; i++){
if(bullets[i] != null && bullets[i].isActive()){
for(int j = 0; j < enemies.size(); j++){
//check for collision!
if(bullets[i].getSprite().collidesWith(enemies.get(j).getSprite())){
//-snip-
break;
}
}
}
}
I'd be interested to see what you're doing around the time you invoke onHandleObtainItem to get a bullet from the pool.
Would it not make sense to set the sprite position to some offscreen value when you invoke onHandleRecycleItem, something like:
#Override
protected void onHandleRecycleItem(final Bullet pBullet) {
pBullet.disable();
pBullet.getSprite().setPosition(-1, -1);
}