[Q] Accelerometer question / algorithm - Android Software Development

I want to detect different levels of acceleration in a vehicle. A requirement is a fixed mount in the vehicle. The mount can be mounted under any angle that is preferrable to the user. Also the mount doesn't have to face forward in alignment with the vehicle.
Now, what I've done so far is, calibrate the "stand still" situation, by reading the x, y, z g-forces of the accelerometer, during a certain amount of time, and then getting the averages of it. I will call these variables gBaseX, -Y & -Z
Once that is done, I want to calibrate the "straight line forward" situation, what I do there is show a screen with a button "start acceleration" and "end of acceleration". This now works by reading the accelerometer, directly subtracting the gBaseXY&Z. During the acceleration, I will search for the highest total G-force. When that's done I save the result in variables gAccelerationDirectionX, -Y & -Z.
Now comes the hard part: (And maybe I should ask this on a math forum or something anyone have suggestions?).
In the main logic of the app I want to use the gAccelerationDirectionX,Y&Z to
'rotate' the axis of my read actual values.
E.g. I have determined in my app that:
gBaseX = 0.15
gBaseY = -0.27
gBaseZ = -9.57
After that I accelerate over a straight line (having the phone mounted a weird angle) and I would get for instance:
gAccelerationDirectionX = -0.55
gAccelerationDirectionY = -0.70
gAccelerationDirectionZ = -0.03
When I move the phone in a straight line forward in the same position, I get those readings:
gReadX = -0.38 (After it's corrected with gBaseX)
gReadY = -0.94 (After it's corrected with gBaseY)
gReadZ = -0.27 (After it's corrected with gBaseZ)
How can I correct the "axis" of this? It must be a formula which use the gAccelerationDirectionX,Y&Z variables. I don't want to use anything like the phone orientation.
What the result must be is something like this:
gCorrectedX = 0 (meaning no left or right movement)
gCorrectedY = -? (meaning acceleration)
gCorrectedZ = 0 (meaning no up or downward movement)
I hope I've made myself clear
Please help on this algorithm.

The easiest way I can think of to do this would be to convert to a spherical coordinate system. Your reference acceleration vector will then be a magnitude and direction relative to the orientation of the reference frame by two angles. You can then convert any other acceleration vector back to a relative Cartesian system by normalizing your new vector with these reference angles.
See:
http://en.wikipedia.org/wiki/Spherical_coordinate_system

Why would you not use LBS (Location Based Services)? Very powerful stuff in there
There is really no such thing as "straight-line" movement...as infinitesimal as it seems, we are always moving in some type of arc. It's the result of living on a spherical object.
Some good reading here: http://en.wikipedia.org/wiki/Great-circle_distance
Calculating instantaneous speed is Calculus I reading. Calc I is why Jeff Gordon lost the Bristol race and Brad K. won it

Rootstonian said:
Why would you not use LBS (Location Based Services)? Very powerful stuff in there
Click to expand...
Click to collapse
Since the OP is using the accelerometers, I would assume that he or she needs a lot finer resolution than you can get with a GPS.

Rootstonian said:
Why would you not use LBS (Location Based Services)? Very powerful stuff in there
There is really no such thing as "straight-line" movement...as infinitesimal as it seems, we are always moving in some type of arc. It's the result of living on a spherical object.
Some good reading here: http://en.wikipedia.org/wiki/Great-circle_distance
Calculating instantaneous speed is Calculus I reading. Calc I is why Jeff Gordon lost the Bristol race and Brad K. won it
Click to expand...
Click to collapse
Oops. You added stuff. I imagine that the OP could safely ignore the earth's curvature in the sort of calculations I think he or she is trying to measure. I remember back in the 1980's when I used to drag race, there was a very expensive device that some racers used when the track was closed that could very accurately measure quarter mile times (as accurately as the traps at the track) all based on accelerometers.

Related

[PRJ] Inertial Navigation Possible

Hi all,
Since the Touch Diamond has accelerometers in the X Y and Z space axis, is it not possible to try and create an Inertial Navigation System. For those who are not sure, it INS uses the accelerometers to assess the movement of the device. So long as the user states the start point and north orientation do you think it possible to do it and to create the software. If it is, then we could do without GPS, afterall, aircraft have used INS for a long time now (albiet theyalso used a gyro). It's not as accurate as GPS but it'll certainly work as a navigation device.
Any comments or help in doing this?
Some GPS systems use inertial navigation for better positioning when in tunners or low coverage areas.
The idea could be simple, a program that read the GPS data, mix them with the G-Sensor data and (why not!) with the TMC data recovered from the FM radio and publish them in a new virtual serial port.
But doing it is not so simple
I think its not dificul to trie i already post the idia in other forum, get the gps data, mix whit the acc, and start counting speed changes in forward . left/right and i think we can get a 600m bonus before black out.
600m whit simple formulas, complex ones give you much more.
Ikari said:
I think its not dificul to trie i already post the idia in other forum, get the gps data, mix whit the acc, and start counting speed changes in forward . left/right and i think we can get a 600m bonus before black out.
600m whit simple formulas, complex ones give you much more.
Click to expand...
Click to collapse
The idea is simple yes - implementing such system is a lot more difficult than one could imagine though. It'll only be accurate for a short distance, before the errors become so large it's not feasible (due to double integration).

[APP] Co-Ordinates Application - Final [updated now for droid too]

Hi All Site Engineers out there,
I had searched for ages to find an app for windows mobile devices that quite simply calculated either new grid co-ordinates from a know point given its bearing and distance or to calculate a bearing and distance given two known points.
I had written a program for a personal computer using (BASIC) but why carry a phone and a computer with you on a building site!
I couldnt find one that was simple, meaning less mistakes when "in the field", so Gino wrote me one. Feel free to download, use and change. Many thanks to GinoAMalone for this simple app, so please donate to either him or XDA of course
The app has a simple home screen giving you 3 choices:
1. New Co-ords Calculator
2. Distance & Bearing Calulator
3. Pythag Calculator
The app is tested on Touch Cruise 6.0 & 6.1, Prophet 6.1. & Blackstone 6.1 & 6.5, Touch Pro & HD2.
Other devices not tested so please give feed back & Screen shots to come.
UPDATE: December 2010: This App has now been written for Android also, please head over to this for info and give thanks to Captainkrtek & Scary Alien
How's this...
Here's a first pass at the app. This sounded like an interesting challenge and something to get me started learning more of WinMo programming. This is my second or third app for .Net. This is written in C# and should only need CF .Net 2.0.
I still need to clean up some rough edges. Right now it doesn't do anything about formatting the output how you requested and most input errors will cause it to crash.
I'm not sure if it's using the same distance algorithms you used. I got my code from http://www.movable-type.co.uk/scripts/latlong.html and http://bryan.reynoldslive.com/post/...2c-Cardinal-Direction2c-Distance2c-and-C.aspx. The algorithm I used is called the spherical law of cosines on the first link.
But, let me know if it looks like the sort of thing you need. I'll PM you with my email address.
Thanks,
Gino A...
Update [20 mins later]: I had a boneheaded mistake in the CAB. Fixed now.
GinoAMelone said:
Here's a first pass at the app. This sounded like an interesting challenge and something to get me started learning more of WinMo programming. This is my second or third app for .Net. This is written in C# and should only need CF .Net 2.0.
I still need to clean up some rough edges. Right now it doesn't do anything about formatting the output how you requested and most input errors will cause it to crash.
I'm not sure if it's using the same distance algorithms you used. I got my code from http://www.movable-type.co.uk/scripts/latlong.html and http://bryan.reynoldslive.com/post/...2c-Cardinal-Direction2c-Distance2c-and-C.aspx. The algorithm I used is called the spherical law of cosines on the first link.
But, let me know if it looks like the sort of thing you need. I'll PM you with my email address.
Thanks,
Gino A...
Update [20 mins later]: I had a boneheaded mistake in the CAB. Fixed now.
Click to expand...
Click to collapse
Hey Gino,
THanks for the app. have given it a test and come up with error on coords and vector parts:
"latitude value cannot be greater than 90"
ALso suggestion, can you also have metres and millimetres as units too? sounds daft maybe, but if working out bearings and new co-ords for setting out a building units would be in metres or mm. This woud prob explain the latitude error.
Also when im inputting in the bearing how should it be inputted? like this: 95'32'25' ?? or have you done in decimal.
Ok, after thought. I know the reason why the latitude error occurs, its beacuse the co-ords input is in degrees, minutes, seconds. I would need an option for co-ords based in meters for example, easting=500.735 northing=345.321 This is important when calculating results for buildings setting out. Can you please put an option in for this, or even purely base the program in metres/mm rather then degress. Obviously the bearing would still need to be in degree, minutes & seconds.
Thanks Gino, the app looks great and im sure with minor tweaks you will have it running in no time!
What about a simple GPS related program which allows you to put in waypoints? It'll give you the distance, heading, etc.. Most of them you can also do offsets of X distance and Y heading, etc..
They do not imply having or using the GPS. Generally you turn the GPS on if needed, but you could use the coordinate related stuff without it.
khaytsus said:
What about a simple GPS related program which allows you to put in waypoints? It'll give you the distance, heading, etc.. Most of them you can also do offsets of X distance and Y heading, etc..
They do not imply having or using the GPS. Generally you turn the GPS on if needed, but you could use the coordinate related stuff without it.
Click to expand...
Click to collapse
Khaytsus thanks for your thoughts. I dont think the GPS is the way to go with this. Yes it would calculate distance and bearing, but i need to input grid line references (co-ords) like 500 metres east, 200 metres north. the calculations in my basic code are based on a 2d grid line system. the app would not need to to use gps. Sometimes when working out co-ors or bearing and distance for setting out a building, the distance from 2 given points could be 100 metres but anything as little as 3 metres. GPS may not provide 1mm accuracy.
If my code was used in the app, it would work, i know it does as i have used it many times before. dont think about curvature of the earth or lon & lat think more grid lines like a flat map. see next post for example.
BASIC - code
Ok my BASIC code below, also attached is a simple (apols) jpeg of what the app should be doing. You will see a red house situated in a grid line system, with its grid co-ords above the relevant point. Hope this is of some help to Gino and anyone else
Code:
To Calc New Co-ords
10 print "New Co-ords"
20 input "Easting=",A, "Northing=",B
22 input "Bearing=",C, "Distance=",D
30 F=sinC*D+A
40 G=cosC*D+B
50 set F4
60 print "Easting 1=";F; "Northing 1=";G
65 goto 22
70 End
To Calc Distance & Bearing
10 print "Bearing & Distance"
20 input "E0=",P,"N0=",Q
30 input "E1=",A,"N1=",B
40 C=A-P
50 V=ABS(C)
60 D=B-Q
70 W=ABS(D)
80 if C=0 then 210
90 if D=0 then 260
100 if C>0 then 160
110 if D>0 then 140
120 E=(ATN(V/W))+180
130 goto 300
140 E=(ATN(W/V))+270
150 goto 300
160 if D>0 then 190
170 E=(ATN(W/V))+90
180 goto 300
190 E=ATN(V/W)
200 goto 300
210 ifD>0 then 240
220 E=180
230 goto 300
240 E=0
250 goto 300
260 ifC>0 then 290
270 E=270
280 goto 300
290 E=90
300 H=(C~2+D~2)~0.5
310 print "Bearing=";DMS$(E)
320 print "Distance=" H
330 End
please note symbol "~" means to the power of (could not find the proper symbol on my keyboard)
also DMS$ = degrees minutes seconds (the new bearing should be displayed in this format, not in decimal)
ATN = (shift tan on sci calculator)
ABS = (shirt LOG on sci calculator)
Pythag Calculation
10 print"Pythag"
20 input "side A=",A,"side B= ",B
30 C=(A~2+B~2)~0.5
40 print "Hyp= ";C
50 goto 10
60 end
timmymarsh said:
Khaytsus thanks for your thoughts. I dont think the GPS is the way to go with this. Yes it would calculate distance and bearing, but i need to input grid line references (co-ords) like 500 metres east, 200 metres north. the calculations in my basic code are based on a 2d grid line system. the app would not need to to use gps. Sometimes when working out co-ors or bearing and distance for setting out a building, the distance from 2 given points could be 100 metres but anything as little as 3 metres. GPS may not provide 1mm accuracy.
If my code was used in the app, it would work, i know it does as i have used it many times before. dont think about curvature of the earth or lon & lat think more grid lines like a flat map. see next post for example.
Click to expand...
Click to collapse
Aha, okay. I was thinking larger scale, less precision, etc. I have to 'deal' with northings and eastings every time I look up benchmark documentation and I always have to figure out how to convert it.. Grid, datum, etc..
Hi Guys,
Any updates? Please dont think im impatient, im certainly not, just willing to help in anyway i can
Regards.
timmymarsh said:
Hi Guys,
Any updates? Please dont think im impatient, im certainly not, just willing to help in anyway i can
Regards.
Click to expand...
Click to collapse
I didn't get as much time last weekend to make progress as I hoped. I did do some cleanup of the UI and add several more units. I believe there are no more circumstances where poorly formatted input causes crashes. I also eliminated the "calculate" button and have it calculate whenever enough info is available. The biggest motivator for this was so the UI works fine with a SIP open.
I still need to look at formatting the output and spend some time with your algorithm.
Thanks for the challenge. this is working as an excellent vehicle for me to learn about .Net CF programming.
try G7toCE
http://www.gpsinformation.org/ronh/
this javascript page will run offline in PIE
http://williams.best.vwh.net/gccalc.htm
GinoAMelone said:
I didn't get as much time last weekend to make progress as I hoped. I did do some cleanup of the UI and add several more units. I believe there are no more circumstances where poorly formatted input causes crashes. I also eliminated the "calculate" button and have it calculate whenever enough info is available. The biggest motivator for this was so the UI works fine with a SIP open.
I still need to look at formatting the output and spend some time with your algorithm.
Thanks for the challenge. this is working as an excellent vehicle for me to learn about .Net CF programming.
Click to expand...
Click to collapse
Gino,
Thanks for the heads up, the app is comin on great. Did you understand my rarther poor explanation of what i meant about inputting the grid line co-ords and did my crude diagram help? I am very thankful and glad i am able to help stimulate you grey cells Get back if i can be of any help please.
Hi All,
Does anyone else want to have a go at this, seems to have fallen by the wayside
As stated in the first post, i will help in any way i can, test and donate of course!
Good luck!
Take a look at this try.
GinoAMelone said:
Take a look at this try.
Click to expand...
Click to collapse
Gino,
Well done my friend, i think you have cracked it. I will test it for accuracy when at work monday with results i know are correct (simple tests like 500.000E & 200.00 N with bearing 90 and distance of 100) work fine.
I see the bearing is entered in decimal, which is not a problem, but it is favourable to enter it in DMS (saves having to calculate this before hand) for example 45o30'00'' (which is 45.500 in decimal)
Really brilliant and im very pleased, let me know if the DMS thing is possible? Cheers Gino.
The labeling of the bearing fields isn't entirely clear. But, it's looking for Degrees, Minutes and Seconds.
I'm pretty sure I'm doing the DMS conversion correctly, but don't trust it till you've tested it. Let me know if it's right.
Thanks.
Gino,
Many thanks my friend, the app with the bearings works very well (dms works). One final thing, could you set the eastings and northings to be rounded up to 3 decimal places for example 500.216 and not 500.21578767.
That would make the app perfect
Well done, you know there is market for this app if you are interested?
I rounded all outputs to three digits.
I've also put a BSD license on this. So, you're free to share it with others if you want. Also, they'd be free to make changes.
If anyone wants to tip me for it, I won't object.
I can't figure out how to do the cute little PayPal button in an XDA post. https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3275014
Congrats to a sucessful app Gino, get that button working and i will happily donate!! I will have a look and see if i can get it working for you!
I too am a site engineer and produced an excel spreadsheet to calculate bearing and distance about 10 years ago. I have just tested and it works on the touch pro so I have attached it here in case it is of use to you.
dougalls said:
I too am a site engineer and produced an excel spreadsheet to calculate bearing and distance about 10 years ago. I have just tested and it works on the touch pro so I have attached it here in case it is of use to you.
Click to expand...
Click to collapse
Kool, nice one Dougalls!! the more the merrier!! Have you tested Gino's app? I did some brief testing and it appeared to give accurate results, but test it and let me know on here please!

Incident Light Meter possible for Blackstone?

Hi since the Blackstone has a light sensor, just wondering if it is possible to use it for film to control aperture and color temp such as the Minolta Light Meter?
http://www.comparestoreprices.co.uk/camera-accessories/minolta-lightmeter-flashmeter-vi.asp
i know it might now work the same way but i think i could work.
I'm using PCINE and PCAM and they are brilliant but would love to have something like this rather than shelling 2000 grand on a light meter.
i'm guessing no?
Can't remember where I saw it but pretty sure you can atleast get the HD to give you a readout in lux or lumens. Could have been an additional program.
But I'd recomend the Flashmeter IV or V can usually get it for a half decent price on ebay - sometimes cheaper to get it from abroad. . .
rather than lux values, would there be one to give out aperture values like most incident light meters?
Luxmeter for HTC
Take a look at pocket-kai's Luxmeter for Touch HD (http://www.pocketkai.net/asp/en/details.aspx?488). Works on my Touch HD.
thanks so much vargamic, however, do would you know if one would exist to tell aperture readings?
If you can get it to read lux value then you can convert it to EV steps and work from there. There's a program here http://www.intl-lighttech.com/library/calculators/evluxconvert that works on PC, shouldnt be that difficult converting it over - I think - or more longwinded use the following formula in a scientific calculator app (copied from a forum at Convert-Me.com)
The definition of EV=0 is an exposure of 1 second at f/1 using ISO 100 film, or any equivalent thereof (2 seconds f/1.4, 4 seconds f/2.0 etc)
The technical definition of Ev is 2Ev = LS/C.
Where:
• Ev = the exposure value
• L = field luminance
• C = Exposure Constant - If you use candelas/ft2, it is 1.3. If you are using LUX (candelas/m2), it is 12.5.
• S = film speed following the ISO standard
Now, as far as where I think you're trying to go with this... Consider that 2ev = A2/T
where:
• A = the f-stop number of the aperture
• T = shutter time in seconds
Combining these two; EV = log2(A2/T) = log2(LS/C) - This is perhaps the most vital formula there is for understandiing the basics of the mathematics behind photography. (From poster called Knight)

[GUIDE] Best Practices for Android Java

During recent development of my app (see my signature) I focused a lot on how to optimize the already working code further in order to make it smoother without loosing quality or using too much memory. Well I came across some mistakes I had made in my early days of developing Android so I figured it might help the beginners and intermediate developers. So here I collected my suggestion of common mistakes and best practices in Android. Let me start by making you familiar with a motto which you might know if you have developed Perl before.
TIMTOWTDI
There's more than one way to do it, in short TIMTOWTDI is a well known aspect of the Perl language, which aims at giving programmers the freedom to choose their way of doing things. It “doesn't try to tell the programmer how to program.” Well it does have various disadvantages such as possibly messy code and barely readable code, but it offers programmers to use their preferred style.
The only reason why I'm bringing this up here is because it helped me a lot to think that way, since TIMTOWTDI sometimes applies to Java as well and a way somebody else is preferring might not be better than your own way. But it is never bad to have a look at how others code the important tasks and sometimes the performance or readability gain is tremendous. So I'm not telling you to exactly use my way, but advice all beginners to perhaps rethink their code .
Tweaking Android Apps
The first step is always to look at what the awesome Android documentation says about performance, so I can more than advise you to read this straightforward article about what to do and not to do on Android. It covers some very important performance issues like the expensive object creation and method invocations. It is crucial to follow those basic rules while trying to develop fast Android apps. I will cover and further explain some of their suggestions here, but will try to also look at them from a developer's perspective who cares about readability and simplicity.
Now onto the code, let's start with some basic Java practices and then move on to some more Android specific styles and improvements.
Looping wisely
Often it doesn't matter if you write slow code and lose 1 or 2 milliseconds but especially in loops and everything that can be called once a frame you suddenly loose a lot of time. Thus it is crucial to especially pay attention to those repeated parts of your code like the onDraw() method in your custom view implementation (see last section) and start optimizing there. Let's take a look at what kinds of loops we have, the slowest first and the fastest ones towards the end:
while
The while loop is probably the first complex programming structure you learned, and just writing
Java:
success = false;
while (!success) {
trySomething();
}
is extremly readable but can potentially run forever or longer than you need it to. One aspect which is often overlooked is that the condition inside the parentheses will be checked on every run of the loop! So even a simple string.length() call can be optimized (if the String won't change) saving its length as an int and then using that in the condition block.
Its unpredictability and lack of possible tweaking done by the JIT makes the while loop a tool that needs caution but is sometimes crucial to complete the task. Just make sure you only use the while loop if there is no way to predict when your condition is false. For almost all other cases, you can do some calculations and use one of the iteration loops:
for (traditional)
This one is widely used like this:
Java:
for (int i=0; i<getSize(); i++) {
doSomethingWithIndex(i);
}
Even though this is more predictable, consider how the program will run: Before every execution of the block, the condition i<getSize() has to be checked, so getSize() is called (and an Android, method calls are expensive!). You could now think, alright, we'll just cache our size upfront like we did with in our while loop, but examine the following fast example of iterating through an array:
Java:
for (int i=getSize()-1; i>=0; i--) {
array[i] = getNewValue(i);
}
The trick is starting at the last value and then iterating backwards until 0 is reached. This may not work at all times, where the order of the blocks may be important or it is redundant due to having a fixed end value, but it saves both method calls and memory usage needed to get the last value before going into the loop.
But that is hardly readable and you often have to rethink because of looping backwards, so we can do better can't we?
for (iterator-based: “foreach”)
Dealing with an array, List or other collection of data you can easily do something for every part in that collection using the foreach loop:
Java:
for (Object item : objects) {
doSomething(item);
}
That is the fastest way you can iterate through any kind of iterable collection because it can be heavily optimized by the compiler and is also simplistic and readable. The only problem here is that you don't have the index of the item you're getting and you can't write data to the collection. To accomplish that one of the slower for loops must be used.
Keywords do matter
This is a minor one that is overlooked often. Beginners in Java mostly don't use keywords and access modifiers like “private”, “public”, or “final”. That is fine since we all love simplistic code don't we? And an honest word, if you don't write a library or work on your code with a big team, you don't have to know much about the access modifiers, but if you want to, there is always the Java documentation. But the “final” identifier is actually pretty important to both the ones reading the code and also the compiler, since it can just insert its value into the references. That means, that whenever you declare an instance variable, think about if it is likely to change or if you can declare it as final. Within methods, making use of the “final” keyword does not really change much for the compiler, but it sometimes helps you make a clearer design so you directly get a compiler warning whenever you're trying to change a final variable's value.
A side note on making “static” variables and fields – I wouldn't recommend that on Android unless you know what you're doing or you're using it together with “final”. A “static final” instance variable is the best way to declare constants in Android because the compiler can replace it fast and ART can replace it during the install of your app!
Strings are special
Let's talk about something that is fundamental to Java – the String object. Well it's not a real object since it is actually immutable. That means a String can only be created or collected by the garbage collector, it can't be changed (which is very important since object creation is god damn expensive in Android so it would make our apps pretty slow)! Wait, then what happens if I call one of the awesome methods in the String class like substring() or replace()? And here comes the downside: These methods have to create new Strings and the old one is collected by the garbage collector. While this might be totally alright if you're just parsing some basic user input, if you need to perform some heavy String operations like many substrings, a whole lot of unused garbage and overhead is created. This doesn't only mean that you are temporarily using a lot of memory, with the garbage collector needed to kick in it also affects your performance.
So how do we get around this problem? Luckily there is a Java class which can do almost the same as the default String implementation, the StringBuilder. This class will hold a char array with all the chars you had in your String. The class can take care of managing that array like initializing it with a default length of 16 and creating a larger array once you have more characters that would fit into it. Take a look at the constructors as well – with new StringBuilder(length) you can directly make that array as long as it needs to be and with new StringBuilder(string) the array is instantly filled with the string. The big advantage the StringBuilder is that it can modify the array instead of having to create new Strings every time. If you're finished with the heavy modifying, just call toString() to get the String back.
If you want to read more about it, here's a nice article.
Android-specific tips
Bundling is better than trundling
Let me explain this with an example: Let's say you are dealing with some data concerning persons so you are saving their name, age and gender. In any object oriented language like Java you would create a wrapper object holding that data:
Java:
public class Person {
private String name;
private int age;
private boolean isMale;
public Person (String name, int age, boolean isMale) {
this.name = name;
this.age = age;
this.isMale = isMale;
}
// additional getters and setters go here.
}
Of course you could do that in Android as well, but you will encounter this problem: What if you need to save your Person array or need to pass a specific Person to another activity? Well, you have a few options available:
You could override the toString() method in Person so it contains all its data and parse your array into a String array manually. Then another constructor will be needed to get the data back from the String using our beloved String operations. But there is still the problem that when you want to add data to the person like height later, you have to reconsider the toString() method and it's constructor counterpart.
Alternatively, wanting to integrate it better into the system, you might want to implement Parcelable in the Person class. That way you can directly put person extras to your Intent or save it to SharedPreferences. But that seems like more work if you want only a simple container for your data. Once you need have a more complex class it might be advisable to make it Parcelable (perhaps using the Android Studio plugin, thanks @nikwen), but let's start with an easier apporach here.
This is how I do it: I use a Bundle instead of a person class to store all the needed data. The Bundle class already implements Parcelable and and simplifies adding data for you. What is more, you are probably already familiar with it since you get one calling getExtras() on the starting Intent of your activity! Now back to the example, this is how it would be done:
To not get confused about all the keys you need, let's create a class containing some static final keys:
Java:
public class Person {
public static final String NAME = “person_name”; // will contain a String
public static final String AGE = “person_age”; // will contain an int
public static final String ISMALE = “person_ismale”; // will contain a boolean
}
While this is only needed for consistent keys, here is how you would create the Person:
Java:
public Bundle getPerson(String name, int age, boolean isMale) {
Bundle person = new Bundle();
person.putString(Person.NAME, name);
person.putInt(Person.AGE, age);
person.putBoolean(Person.ISMALE, isMale);
return person;
}
Similarly, you can get one or more stored values of the Bundle using one of the person.get...() methods. Furthermore, instead of creating an array of Persons you can now create a Bundle of persons using bundle.putBundle()! You just need to find the right key-scheme, here you could either provide an id for each person or just use their names as key (although the key array has to be passed seperately). And what do we get from all this? Well we can now just call intent.putBundleExtra(person) and voilà, we've passed it to another app component.
Resourcing is not outsourcing
One of Android's big advantages is its exceptional resource system. The fact that all your Strings and values are saved in a separate xml file makes your app not only easier to translate, but also keeps your code cleaner and lets you have a complete overview of what amount of constants you use. But you can go further than that. The resources allow for entirely different configurations depending on screen size, resolution, orientation, location and api-level! To learn more about how this can be done, head over to the Android devoper guides.
One thing I wanted to highlight is that it used to be quite hard to make a consistent interface and still support Android 2.1 and above. That is not the case any more since we now the the continuously improving AppCompat library. It is even useful for apps targeting only ICS and above because it contains bugfixes and improvements for those versions as well. Using this library is the best way to get the holo theme and its ActionBar in your app, although if you could also try ActionBarSherlock to accomplish the latter.
Click to expand...
Click to collapse
Think D.I.P
So what's this strange thing they call dip? And why do we even bother using it, we have had pixels (pix) since the beginning of programming. First things first, try to avoid pixels on Android. On almost every other platform using the smallest unit that's available is a great thing in layouts, but not so much on Android where apps can run on devices as small as a smartwatch up to 2k tablets. The problem is that you can't be sure if a single pixel is as small it can barely be seen or as large as a few millimeters. That is why we have the standard-sized density-independent-pixels (dip or dp in short) where 48 dips are roughly equivalent to 9mm, the convenient size of a touchable area (e.g. a Button). Great, then how many dips is my screen wide/high? That is not an easy question since it varies as well. Phones usually have around 320dips (at the smallest width) but larger screens can fit more so on tablets you have 600dips or more. Try to understand this documentation and don't be confused with the abbreviations dp, dip and DPI – dp and dip are the same and DPI is dots per inch (similar to pixels per inch), the screen density!
The reason why I'm bringing this up here is advice you to understand and always think in dips, never in pixels. Even on a Canvas, where there is only methods for pixels, don't think “ok I draw my text 20 pixels from the left edge and 50 from the top so canvas.drawText(“text”, 20, 50, myPaint);”. Start to think in dips: “I have to draw it 16dip from the left edge and 24dip from the top!” But how to convert it to pixels so it can be actually drawn onto the Canvas? It really is a shame there is no method in the Context class to directly convert dip to pixels, so here's the one I use:
Java:
private DisplayMetrics displayMetrics;
/**
* Converts a given dip (density independent pixel) value to its corresponding pixel value.
* @param dips The dip value to convert, as float.
* @return The pixel value, as int.
*/
private int dipToPix(float dips) {
if(displayMetrics == null)
displayMetrics = getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dips, displayMetrics);
}
And a side note on sp, that is the scale-independent text size. Try to use it as far as you can, for instance in layout resources. But just in case of needing the actual size, use the same method because sp is computed almost the same as dip.
Hyperthreading is not that complex
An app's UI (or “main”) thread is its beating heart, and we don't want to slow down what's powering us if there are other options available, right? If you don't follow the rule of not performing any time consuming tasks in the default thread you'll end up with what all developers fear: The “App is Not Responding” dialog – in short ANR. When users see this, they can get really mad, from just force closing your app and uninstalls to 1-star ratings everything is to be expected. Because it is crucial to know about Threads (and Processes), carefully read the guide in the Android docs about it, pay special attention to the two simple rules in Android threading:
1. Do not block the UI thread
2. Do not access the Android UI toolkit from outside the UI thread
Click to expand...
Click to collapse
Let's take a look at the first one, what are you going to do instead if you need to accomplish an expensive task like web requests, complex calculations, Bitmap operations, database queries and I/O management?
Well the answer is not as simple as it may seem since it depends on what and how often you will be doing that task. A task that is performed once per activity launch is almost always wrapped into an AsyncTask, a class which is heavily optimized and integrated into the system. If you know how it works it is a very powerful tool since it does most of the heavy lifting for you, so let me show you how to do it:
You start by extending the AsyncTask class like so:
Java:
private class MyAsyncTask extends AsyncTask<Param, Progress, Result> { //...
Usual stuff, except for the weird <Param, Progress, Result> thing. It is actually not that hard, these are all just type declarations of variables your methods want to use. Thus, the first one is the type of starting parameter you want. Think of it as if you wrote your task in a separate method without access to any instance variables, what data would it need to work (what params would you pass to it in the parentheses). The second one is actually often unused and is needed if you want to return a var indicating progress on the task so it can be published e.g. in a ProgressBar. Lastly, Result is (you may have guessed it) the type of variable you want your task to return, to be published in the UI as well. A typical configuration would be <Uri, Integer, String> for a task reading a file. Note that these have to be objects, so for primitives you'd have to use their respective Java classes like Integer or Double. But that also means that a Bundle can be used as well so we know how to pass multiple parameters now! And there is another way as well, look at the overridden methods:
Java:
protected Long doInBackground(Uri... uris) {
// do your expensive work here, it runs on a separate thread!
}
protected void onProgressUpdate(Integer... progress) {
}
You'll notice the three dots after Uri and Integer. To keep it simple, treat it as an array, so to access your value call uris[0] and progress[0], respectively. The reason for it being an array is that you can start the same AsyncTask with multiple parameters of the same task:
Java:
new MyAsyncTask().execute(uri1, uri2, uri3, uri4);
This is extremely useful in this case to read multiple files at the same time (don't be confused with the configuration in <> above, the param(s) passed to execute() are joined together in the Param array).
A side note on progress here, if you want to publish progress during your doInBackground(), just call publishProgress(Progress) and override onProgressUpdate() to publish the changes in the UI. Similarly with onPostExecute(), you'll probably want to override that method as well to show your awesome result to the user.
What you need to keep in mind is how AsyncTask is handled internally, read the section “The 4 steps” in the AsyncTask documentation carefully. In fact, doInBackground() is method you need to think about the most since it is encapsulated in a separate thread with no access to the UI or methods in your activity.
Because AsyncTask is limited to one operation and should only run up to a few seconds, there are a couple of other ways to do tasks in the background like a second Thread and helpers such as Handlers. The Handlers are needed to access the UI from the worker threads, still following the second rule above. If you want to read more here's an awesome Vogella tutorial about it.
Fragment-tation
This next one is really a good style of development and can save a lot of work if you provide layouts for both phones and tablets (which you definitely should). The basic theme of using Fragments is to follow the divide and conquer technique, which dates back to ancient time in Babylonia. Thus, the idea is to have one large problem (a user interface and its data for both tablets and smartphones) and split it into multiple simpler ones (in this case having one or more Fragments for each screen on phones). Especially in multi-pane layouts, for instance in the settings app, Fragments are a wonderful tool to avoid the use of two different activities, one for phones and one for tablets. The steps to implement them in your app are pretty simple and there are already some neat guides like the one in the documentation and Vogella's one. An additional use-case for Fragments is to retain an AsyncTask when rotating the screen. You would create a Fragment without layout in the UI and call setRetainInstance() on it. Any expensive AsyncTask can then be started inside that Fragment and is not stopped when the activity is recreated during an orientation change.
Memory-zing is unneeded
But it is not only performance, layout and style which can be improved, think about memory usage for a second. It goes hand in hand with performance since an app which consumes unnecessary memory (garbage) will be slowed down by the garbage collector (GC) thrown in from the system, trying heavily to free some RAM. On Android this is extremely important, since a garbage collector running intensely on a single or dual core processor can really slow down the device's performance. That is also why method calls and object creation are so expensive and have to be used with caution – the overhead they produce bumps up the memory usage. And there is one specific case where you can really have a problem with your memory, a so-called memory leak. What this means is that some part of your app is holding on to a very large object like a Resources or database one or even a whole Activity or Fragment. The problem is that the GC can only collect objects which aren't referenced from an active part of your app so this means it can't be collected and ends up blocking all your available memory. Such a leak is something you want to avoid at all times and there are some awesome tools to find out if you have one. All you need to know about that and memory optimizations in general is covered in this amazing I/O talk, so I advice you to watch that!
Most of the time there is no real need for reducing method calls and object creation because the GC is fast enough. But there is one step where it is crucial to avoid it, methods which are called on every redraw like the onDraw() method of a View, which I'll cover in the following.
Optimizing performance? onDraw!
In your custom views the most important method to think about is the onDraw() method since that is where everything that is visible to the user is rendered. A lot can be gained (or lost) in that crucial step, so try to follow the performance guidelines posted earlier as close as possible. Especially, pay attention to the object creation, creating a few objects in onDraw() might not seem expensive, but the memory footprint and the garbage collector usage will be tremendous. When drawing on a Canvas for instance, a common pattern would be to check if the paint instance variable is null before drawing:
Java:
if(paint == null) {
paint = new Paint();
paint.setColor(drawingColor); // ...
}
That code would typically be placed in the surfaceCreated(), but could also be in the onDraw() if you are lazy and want to keep it in one place. . While object creation is not the only thing you can improve performance-wise, just try to apply the aspects I wrote about in the beginning, especially those loops!
One thing I have to point out about performance before moving on to some links that might help you is that before trying to optimize your app in some arbitrary way (and totally destroying readability) measure upfront. That means use the tools mentioned in this great I/O talk covering graphics and performance to get a sense whether your app is really driving close to that 16 millisecond threshold to get the desired 60 frames. Additionally, it might be a good idea to not only test it on the high end devices (#Nexus5) but also those with a high “resolution/processing” value such as the first tablets with 1080p resolution. Thus, only start tweaking performance if you know you take too much time, then try to nail down the time consuming methods. And always try to improve readability and simplicity, not only for us when posting pieces of your code here but also for yourself: it helps getting into the flow if you see that your code is readable and nice .
Additional resources
If you can spare some time and want to know more about how to build awesome apps and UIs, check out the Android sessions at Google I/O 13, especially the Android UI design talk and the Android Protips 3 (but the first and second Protips were great as well as the beginner's talk from 2010). There is also the Android developers blog and the Vogella tutorials. As usual, the Android developers page is always a great resource and Google is still your friend (use “android dev” together with your question to get better results). For more info about general Java performance improvements, I found this nice pdf outlining how you can improve your code even further.
Click to expand...
Click to collapse
</guide>
Anyway, that's about it. It has really become a way too long collection of links and videos so I hope it was useful and perhaps made you rethink your code . If you've got any comments or suggestions feel free to post them here, helps a lot.
This guide was featured on the portal on February 4th (thanks eagleeyetom!)
To be continued...
One Thanks isn't enough!
Great writeup! Thanks!
Wow man such a good information muchly appreciated great job!
Sent from my Nexus 4 using Tapatalk
Wow, great collection of tips and tricks. Much appreciated!!
Handy tips
awesome job dude.was really helpful!!!!!:good:
Great article! Please keep going. :good:
Simply awesome...great work.
Sent from my SAMSUNG-SGH-T989 using Tapatalk
Thanks for this great writeup! The android specific tips are really interesting.
Great guide (again). :good:
Just one thing I want to add: I still prefer wrapper classes which implement Parcelable. In fact, it's not that difficult and you should be able to do it in a few minutes. Have a look at the example code here: http://developer.android.com/reference/android/os/Parcelable.html That's it.
I've also found an IntelliJ plugin for that purpose (but I haven't tried it yet): https://github.com/mcharmas/android-parcelable-intellij-plugin
That wasn't meant to degrade your work though. It's an amazing guide.
nikwen said:
Great guide (again). :good:
Just one thing I want to add: I still prefer wrapper classes which implement Parcelable. In fact, it's not that difficult and you should be able to do it in a few minutes. Have a look at the example code here: http://developer.android.com/reference/android/os/Parcelable.html That's it.
I've also found an IntelliJ plugin for that purpose (but I haven't tried it yet): https://github.com/mcharmas/android-parcelable-intellij-plugin
That wasn't meant to degrade your work though. It's an amazing guide.
Click to expand...
Click to collapse
Good point and thanks!
Having a closer look at it, implementing Parcelable is not that much work and it would definetely be the more advanced way. Bundle is basically just another layer that makes things a bit easier for the start (and no typecasting) and I found that it can really shine with a singleton class holding the keys and the getters and setters (which have an additional param, the Bundle). For a beginner who just wants to save some values (possibly keeping the keys in the activity or even hardcoded) using the Bundle is a lot easier. Yet again, TIMTOWTDI
Thank you!
Hey Simplicity, after few years there is something you would change? Maybe dagger2, kotlin, rxjava changed the game, as really open question what do you think about?
The question might be a bit off topic, i am working on a root app development but i am pretty confused that how should i get the busbox commands to run on button press, i have given the path and everything but how am i supposed to create a busbox file in data/data/package/files.
This part is confusing me I am not able to create that file, should i keep it in res while creating the app ? And also one last question, from where can i get that busbox file with all its command. Please help, I'm sorry if this is the wrong forum I'm new to posting questions and also to app development .

[Q] Using accelerometer to perform actions

So I'm in the preliminary stages of doing an experimental HCI project for a capstone course. I have no android (java) coding experience but I do have background in C++, Python, and QT so I'm hoping to be able to pick up java relatively fast.
Anyways, the idea of my project relies on being able to take accelerometer readings to determine device tilt in the X and Y axis (as documented in the SensorEvent section in the Android Reference guide) and pipe it into an app such as Google Maps to perform a certain action (in this case I'm tying to pan the map).
I know there are games that use the accelerometer to determine tilt that exist out there (eg. Minion Rush when collecting bananas on the unicorn) so it should be well documented I'm hoping....
Is it possible to make an app that's a "skeleton" over Google Maps and when the app detects certain orientations of the phone to execute the appropriate function. eg. To pan in the direction the phone is tilted? It looks like there's the "CameraUpdateFactory.scrollBy(float, float)" function to pan the map that takes 2 floats as arguments can I take the accelerometer readings (X and Y) and plug them into this function? I'm somewhat familiar with QT signals and slots, is there something like that that exists in Android app development so when the phone is tilted past a certain angle it will emit a signal that can be captured and plugged into the above function?
Also can anyone point me to any HCI research papers that may deal with this topic or keywords to be searching for?

Categories

Resources