Wednesday, September 8, 2010

Using arraylists properly with android


Late post so ill keep it short.

Android development hinges on being efficent with memory. Declaring objects in a loop is not efficient. If you use a foreach loop with an arraylist, you are creating an iterator each time you run through the loop. Instead, loop through your arraylist using a for loop like this:

for(I =0; I<mylist.size();I++){

       Myobject=mylist.get(I);

...

)

That will prevent memory leaks and keep your game or app running smoothley since the garbage collector will be swift.  See my other post for how to track all memory leaks while developing with android.

Tuesday, September 7, 2010

How to Find Memory Leaks in Android Applications

While working on a game, I noticed the Garbage Collector was causing significant delays while cleaning up memory. The GC is used by Java to free up memory that has been taken by objects that your program can no longer access. Since a game is inherently developed in a loop, it is very easy to create unnecessary objects over and over. Once these take up too much memory, the GC must run to free up more memory for the App. Unfortunately this can cause a pause in your game, or even a simple app.

The android SDK comes with a great tool to find these memory leaks.

-Just go to your tools directory under your SDK folder, and open the ddms. Make sure you don't already have eclipse running, as only one debugger can attach at a time.

-Run your application on your attached device (your phone)

-Select your application in the application list on the left side of the ddms

-Select the "allocation tracker" tab in the ddms

-click "Start Tracking" in the allocation tracker tab

-Interact with your application on your device for a while

-click "Get Allocations" in the ddms

This will list the memory allications, in descending size. You can click on each allocation and the full stack trace is given, which allows you to determine what line of what class the allocation was made. Very easy! Now go to that line and smack yourself in the forehead for that worthless declaration, and become a greener programmer!

This will make your game/app run much smoother. However, remember that the garbage collector normally runs, so you can never get rid of all GC collections.

original post: Tracking Memory Leaks

Thursday, September 2, 2010

Use Keyboard and Touch Screen Input in Android Game

Just a few things you need here, but this can be really frustrating if you don't know what's happening:
1.You must keep your view in focus when android enters touch mode.
2.Your view must be in focus.
3.You must have an onkeydown listener of some sort.


So to accomplish these, in your View constructor:

1.this.setFocusable(true); //allow the view to be focused
this.setFocusableInTouchMode(true); // allow the view to be focused while in touch mode
2. this.requestFocus(); //set the focus on this view

Then in your View class:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("-------------------->code", Integer.toString(keyCode));
if (keyCode == KeyEvent.KEYCODE_FOCUS) {
//handle focus button pressed
return true;
}
return false;
}

Returning true tells android you have consumed the keypress. Returning false passes it along to other listeners.

Wednesday, September 1, 2010

Angry Birds Coming to Android Friday

Since so many people signed up to be beta testers, the developers of Angry Birds have decided to release the beta version via the Android Market this Friday.

For those who don't know, Angry Birds is the popular iPhone game in which you fling exploding birds into buildings. It has sold millions of copies.

Original post: droid-life

Monday, August 30, 2010

Force Landscape Mode/Orientation Android Development

The best way to do this is in your AndroidManifest.xml file. Just right click and open with the text editor in eclipse. Then add:

android:screenOrientation="landscape"

to your the activity that you want to be in landscape mode. Here's what my androidManifest.xml file looks like, sorry for the poor formatting options on Blogger:

How to Debug via USB Android

This is very easy if you are already setup with adb in eclipse:

1.plug in your device(phone)
2.enable USB debugging on device
3.debug/launch app from eclipse as normal, it will autoselect your device if it is attached.

If logcat isn't working, close and reopen eclipse.

Handle Multitouch Android

Here's the basics you need to know:
- setup an event handler to handle your ontouch events
- touch events have different actions: ACTION_DOWN, ACTION_UP, etc. are used for the first touch event registered. ACTION_POINTER_DOWN, ACTION_POINTER_UP are used for the second touch event. As far as I know Android only supports 2 touch events.
- to use the second event, you must combine the event.getAction() and MotionEvent.ACTION_MASK by anding them as seen below.

First you need to catch your action in your touch event and store it as an int:

public class GameEvent implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
actionCode = event.getAction() & MotionEvent.ACTION_MASK;//this combines single and multitouch
}
}

Then test to see what the event action was:
if(actionCode == MotionEvent.ACTION_DOWN){
}else if(actionCode == MotionEvent.ACTION_POINTER_DOWN )...

from here you can get the X&Y coordinates of each touch, the pressure, and more.

Sunday, August 29, 2010

Free Android Open Source On Screen Joystick

Surprisingly this is the only free on screen joystick I was able to find online that wasn't part of some other game engine. It's fairly straight forward and easy to implement.

The movement calculations were a little complex and didn't work for me, so I replaced them with this in the GameControls class:

//get the angle
//double angle = Math.atan2(_touchingPoint.y - inity,_touchingPoint.x - initx)/(Math.PI/180);

float xSpeed = _touchingPoint.x - initx;
float ySpeed = _touchingPoint.y - inity;

xSpeed *= 0.5;
ySpeed *= 0.5;

_pointerPosition.x += xSpeed;
_pointerPosition.y += ySpeed;

// Move the beetle in proportion to how far
// the joystick is dragged from its center
//_pointerPosition.y += Math.sin(angle*(Math.PI/180))*(_touchingPoint.x/70);
//_pointerPosition.x += Math.cos(angle*(Math.PI/180))*(_touchingPoint.x/70);

UPDATE: I am now using this for a dual joystick implementation. See my post on developing with multitouch in Android.
Here's the download and original forum post: http://trostalex.wordpress.com/2010/07/26/on-screen-joystick-source/

Tuesday, August 17, 2010

Adw.launcher on 2.2 droid


Adw.launcher is sick

After experiencing the slow homescreen loading issues with 2.2, I decided to try out a couple custom launcher apps: launcher pro and adw.launcher.

Launcher pro is pretty handy in its own right, the only knock is that it only has 3 screens. On the other hand, the dock is extremely customizable. You can load and use your own images.

I chose adw because of the 5 homescreens, customizable dock, and smooth interface . I have not experienced any of the slow loading issues I had with the default homescreen on my droid. Definately a keeper, oh and its free.

Htc incredible low on memory, storage


A lot of people have been reporting memory issues with their htc droid incredibles.  The phone is running slow, throwing errors left and right, and the battery lasts maybe two hours. 

I believe this is due to the recent Facebook update for android, and the htc people application that comes with the sense ui. What happens is the facebook application tries to sync all of your contact info from facebook, even when your phone is supposed to be sleeping.  Then something gets messed up in the people app, which holds your contacts, and an exorbant amount of memory is used up. To the point that you're phone can barely function.

The solution to this is to delete or opt out of sync with your facebook profile. Then delete your bloated contacts application.  As I learned the hard way, make sure your contacts have been synched with google before deleting. They may not have even though you thought sync was turned on.  A hard reset is not necessary as some have mentioned.

I asked the Verizon Rep about this when we went to the store to transfer contacts from an old phone back on to the incredible. He said he hadn't heard of the issue. Somehow I got the feeling I wasn't the first person he had told that to.

Blogaway


If this posts, then blogsway is a decent blogging app

Update: Its decent, not great. Force Closes when trying to edit...

Update: latest update through the market fixed fc when editing posts. 

You can also edit blogger posts on your mobile browser by using the edit HTML tab. Don't be scared off if you've never used HTML before, the editor works almost the same as the default. Just type like you normally would.

Best Android Apps

Here are a few of the Android apps that I use on my droid:

Apps

NFL mobile (Verizon only) - Watch what is currently on NFL network live! Free!
Terminal Emulator (Root Users) - Good free terminal for entering commands
Pandora - Streaming radio based on your favorites
Overclock widget (Root)- allows you to set your CPU based on whether your phone is awake or asleep
Tesla LED - mototorch replacement that works with 2.2
Handcent SMS - Unleashes all of the options you could dream up for texting.
Wireless Tether (root) - share your 3g connection with wifi devices
ADW Launcher - Homescreen replacement that gets rid of the laggy 2.2 homescreen that is slow loading icons sometimes. Also allows for 4 items on dock, and 5 homescreens.

Games

iCommando - decent sniping game
Hockey Nations 2010 - Overpriced hockey game, but you might be into it. Nice graphics, gameplay gets old.
Raging thunder lite - Haven't tried the full version.
Amtalee lite - 3d puzzle game
Abduction - Papi jump, but with a cow.
Craigs Race - Awesome game, be sure to calibrate your tilt sensor. Buy the full version for 2 faster cars
Radiant lite - like space invaders/asteroid mixed together
2 player reactor - great game for killing time with someone. See who can touch the screen when it turns white faster etc...
Emulators - There are currently emulators for ps1, game boy advance, super nintendo, nintendo, and most likely sega (haven't seen sega myself). The lite versions of each allow you to play games, just not save them. So you can try them out and see if that's your thing.


Widgets

Power widget - quicker than quick settings
Weather - not weather channel, this comes with your phone and shows you a 24 hour forecast, along with a weekly forecast.

Sunday, August 8, 2010

LogCat not Working in Eclipse Mac

I installed ADT on my macbook pro the other day and everything went smoothly until I tried to debug my app. LogCat wasn't displaying anything. After searching online for a while, the only information I could find was that it just doesn't play nice with eclipse. It seems that when I close my emulator, LogCat throws an error saying it can't attach to the emulator. Then the next time I try to debug, I don't get any logs. Restarting Eclipse fixes the issue, but gets annoying, when I have to restart every time I want to debug.

I also can't seem to get any breakpoints to catch...no logs or breakpoints makes it hard to code.

UPDATE: If you just back out of your live wallpaper, rather than hitting the power button, you can go back to eclipse and hit "debug LiveWallpaperName", this will reinstall the current version of your apk. This way you don't have to detach from the emulator and LogCat will keep spitting out logs.

Saturday, August 7, 2010

WiFi Tethering on Verizon for 2.2 (Froyo) Mac and PC

Edit: This is free. There is no $20/month fee or anything of the sort.

After a lot of trial and error, I've found a wifi tethering app that works on Verizon for root users on Mac or PC.

The "Wireless Tether" app in the app store works great. Just download, run, then click the giant wifi symbol to enable. You may get an error that says something like "started with errors, view the log". The log will most likely say that access control failed. Hit menu and go to access control and turn it on and off (or just off if it was already on). Then go to your computer and join your new network. Currently I haven't tried any security (WEP/WPA), but I'm pretty confident none of the tractors I'm driving by in central Iowa have wifi devices.

This works for Pete Alfonso's Rooted 2.2 ROM based on the test release, on a moto droid, and many other combinations.

Just to reiterate. A lot of people have problems with access control, just toggle it on and off to get things to run smoothly.