The Young Persons Guide...

...to Becoming a Mobile Developer!

The NEW Android Location API

Since the release of the new Google Play Services 4.2, some new APIs have been added and Google have changed their Client API Model. One such API model to be affected is the Location API.

If you are targeting the latest version of Android in your app, you will see that your existing Location API code will now be flagged as being deprecated.

Previously, you would interact with the Location API as follows:

    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, false);
    locationManager.requestLocationUpdates(provider, updateInterval, 0, this);

    ....
    public void onLocationChanged(Location location) {
        //Handle location update
    }

Now with the new Location API, it will look similar to this:

    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(LocationServices.API)
    .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
    .addOnConnectionFailedListener(this)
    .build();

    ....
    public void onConnected(Bundle arg0) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

        ...
    public void onLocationChanged(Location location) {
        //Handle location update
    }

You’ll need to import:
com.google.android.gms.location.LocationServices (thanks Mark)
com.google.android.gms.common.api.GoogleApiClient;
com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
(note it’s the GoogleApiClient, instead of the GooglePlayServicesClient version).

As well as LocationListener, your class also needs to implement ConnectionCallbacks and OnConectionFailedListener (assuming the same class is handling all the callbacks).

Also, onDisconnected() has been renamed to onConnectionSuspended(int cause).

Quick android tip, with ems (setEms())

I recently ran into an issue with my TextView not wrapping text. For some reason, if the line was too long, it would continue off screen.
I tried many different fixes from across the web, but none worked. I thnk the main problem was that I was not using a defined layout in XML, but rather creating the textview dynamically upon encountering an error.

The answer that I finally settled on was the use of em’s (http://en.wikipedia.org/wiki/Em_(typography)). This type or measurement relates to font sizes, and in Android, helps keep fonts and other objects the same relative sizes across any screen size or resolution.
Loosely speaking, a typical screen width in ems, is equivalent to (the screen width / 24). Going back to my original problem, where I had the text go off the screen, I could use this calculation to set a max width of the text view, and manually make it wrap the text.

Since I also target older versions of android, eg 1.6 and 2.2, I had to use an older deprecated method to get he screen width. If you only target newer versionm eg API 17+, you can use the DisplayManager, but if like me, you want to have maximum compatibility, then you can use the following code to set the width of a layout to be equal to the screen size of whatever device you’re on:.

Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
TextView tvError = new TextView(this);
tvError.setEms(width/24);

Android does come with some built in functions to help text wrap, but from some discussions online, I saw that certain themes, eg Holo, can cause issues like what I’ve experienced. This fix only came about because I didn’t want to change from the Holo Theme.

I hope this helps someone.

EDIT:
Since the above method uses deprecated methods to get the width, I think its best to update this post with the current recommended methods of getting both the width, and the height of a screen in Android:


final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int height = displayMetrics.heightPixels;
final int width = displayMetrics.widthPixels;

DDPB error: Cannot find BarDeploy.jar

While testing my apps on my Blackberry PlayBook, I sometimes have to sideload them if I am on another computer without the proper SDK tools installed.

For sideloading, I use a tool called DDPB. It connects to your device, lets you select the .BAR file, and basically handles everything for you.

However, after transferring my copy of DDPB over to another machine, I ran into a problem. When I try to connect to my device, it gave an error: Unable to access jarfile C:\Downloads\BarDeploy.jar

As it happens, I had copied it from a 32-bit machine to a 64-bit machine. DDPB is installed in Program Files directory in Windows, but on 64 bit machines, the 32bit program files directory is called Program files (x86) so when I transferred the backup over, it had the old path and could not find the jar file.

Long story short, if you get this error, just recreate your shortcut to DDPB!

Making games for beginners

Following on from my last post from a number of months ago, RIM were at it again! The latest freebie deal was part of their 36 hour Game Port-a-thon, which was held last week. Rather than give everyone a free PlayBook tablet, they opted for another approach. For every game app that was submitted, they would give you US$100, with further rewards for submitting more apps.

For example, if you submit:
< 3 games, you get US$100 per app, 3-5 games, you get US$100 per app, free PlayBook, 5-10 games, you get US$100 per app, free Playbook, free BB10 phone (first 100 only) 10+ games, you get US$100 per app, free PlayBook, free trip to Dev Conference in Sand Diego (first 10 only) As you can see, its quite the deal. MAKING GAMES
When I found out about this deal, I figured I wanted some of the action. Unfortunately, I have never made a game in my life! Not even a small one! I began searching for Idiots guides to making games and beginners tutorials, and I found some good ones. However, since I only started looking 3 days before this Game Port-a-thon, I didn’t have time to learn all these skills. Instead, I found a clever piece of software called Construct 2 (www.scirra.com). Construct 2 enables you to make relatively basic games, with relative ease. The interface is almost drag and drop. There’s a small learning curve at the start, but once you get your head around it, its very manageable. The games get created based on events, and all of the physics is handled internally so you can get results very fast. See the video below for a quick sample.

Construct 2 demo

I was impressed with Construct 2 actually, so much so that I might even splash out the $100 on the full licence! I decided to try the free version for a night to see if it was any good, read one or two tutorials, and found that it was actually pretty easy to set up and get going, at least for the basics. I didn’t try the more complex games, mainly because I didn’t have time, but also because the free version only allows 40 events per game. This is still a very usable limit, and some very fun games can be made with this. The software is aimed at non-developers and requires little or no coding for basic games. In the space of a few days, I was able to create 3 basic, but half respectable HTML5 games.

Platform Conversion
Contruct 2 also comes with the ability to export your game into an array for platforms. For the Blackberry Game Port-a-Thon, obviously you had to submit the game in a format suitable for their devices. This is why I chose to export my games as HTML5. If you read my previous post about porting Android apps to the PlayBook, you might be aware that RIM created an Eclipse plugin that allows Android developers to port their Android app for use on the PlayBook in about 3 mouse clicks (once configured). Similarly, they’ve also created a tool for converting HTML5 games into apps compatible with their own devices.

To cut a long story short, I was able to create 3 games in as many days, with experience in making apps, but no experience in making games, and I was able to make them compatible with RIMs upcoming BB10 OS. In that time, I looked around for the quickest ways of producing games and the quickest, hands down, was Construct 2.

Also, as far as I know, you can convert HMTL5 games into Windows Phone, Android and iOS formates, so its win/win all round. Construct 2 even comes with PhoneGap compatibility to do all conversion for you!

So, if you want to get started at making games, and haven’t got the programming skills, then try Cosntruct 2. For more advanced games (and programmers) however, maybe Unity might be a better solution!

Free Blackberry PlayBook for Android developers

I recently got an email from a tech magazine I’m subscribed too. Usually they have interesting articles, and all the news in the world of Computers and Technology, as would be expected. However, in last weeks magazine, there was an interesting article relating to Blackberry and their flagship tablet, the PlayBook.

With the imminent release (at time of writing) of their OS2.0 software, Blackberry have decided to give away free (yes, FREE) PlayBooks to any developer who submits an app specifically for the tablet before the deadline. Since the new OS contains its own implementation of Android, which allows Android Apps to run pretty much natively, the offer was originally open exclusively to Android developers. However, with a flood of complaints from existing Blackberry devs, and some noise from iOS and WP7 developers, Blackberry decided to open up the offer to any dev, of any platform, who submits an app before the deadline. Originally the deadline was Monday 13th Feb 2011, but since they’ve had an overwhelming response (over 6000 new devs submitting at least 1 app) since last week, they have extended the deadline. Developers have until Wednesday the 15th of Feb to register as a dev, and until March 2nd to submit an app.

Blackberry PlayBook

There is a bit of small print, however. Not every app will be accepted, and qualify for the free PlayBook. The app has to be of sufficient complexity, and be deemed (by a human tester) to be something that people will be interested in. Examples of apps that will be rejected are: Web Browsers, shortcuts, themes/wallpapers and web apps. They’ll also reject apps with a single function like a “Fart Button” or Buzzer app.

Since I stumbled across this offer, I have submitted my Lotto App, which has been accepted. I even got the email asking for my shipping address, and expect the tablet to be shipped any day now, all for the one time fee of ZERO!! 😀

For anyone looking for info, here’s the latest link with info on the deadline extension:

Free PlayBook offer extended

Technical Specs of PlayBook:
1Ghz dual core processor
1GB RAM
HTML5 / Flash 10.2
Proper multitasking due to QNX
7 inch screen
Weight: 0.9lb/425g
Width: 7.6/194mm
Height: 5.1/130mm
Thickness: 0.4/10mm

1080p Playback
3MP front camera
5MP back camera

New Android app: Medical Student MCQ App

So, after being bitten by the Android Publishing bug with my first app, the Irish Lotto helper app, it’s no surprise that I jumped at the chance to create a 2nd app, when the opportunity came knocking.

My 2nd app, which has just been published, is called Medical Student MCQ App.

The purpose of this app is to aid Medical Students who are in their final year of med school, by asking them a difficult multiple choice question (MCQ) once per day, for the entire duration of their college year. The theory behind it is if they can learn that one piece of information each day, then by the end of the year they should fair much better in their exams!

The idea for the app came about when my brother, who is an experienced paediatrician, asked me if it would be possible to make such an app. I responded positively and we decided to take it from there, with me doing the technical side, and him writing the questions.

For this to be as efficient as possible, I had to write a basic web form in PHP, using a MySQL backend. He could simply fill in the form with a question, 5 possible answers, a radio button representing the correct answer, and a textarea at the bottom for him to fill in the “tip”. The tip pops up when the user gets the correct answer, and gives them an expanded explanation of why the answer is correct.

Anyway, due to time constraints, we had to enlist 2 other doctors to help with the questions. This worked out well as now we have a team of 3 doctors, each with their own specialised field, able to write relevant and helpful questions.

On the client side, I had to choose between including the full database, or forcing the user to download the database on first run. I chose the latter, and for good reason.

Due to the number of questions, it is possible that errors could sneak in somewhere. Rather than releasing a new app version for each spelling mistake, and hoping everyone upgrades promptly, I thought it would be better to have direct control over the content they see every day. To achieve this, I have put in an “update” button in the menu, whereby the user can download the latest database at the touch of a button. This will include all the updated/refined questions, without forcing an app update through the market. Also, if we want to add new questions, or remove questions completely (maybe due to curriculum changes) I can do so through my little web front end that I built for this.

Medical Student MCQ app, available for 4 euro from the Android Market

The app is available now, for a small price of €4, and is available for download from the Android Market here:
Medical Student MCQ App

-Eoin

New Android app: Lotto Helper IE

So, after dipping my toes in the Android water over the summer, I began working on my own app.

I took me roughly one month to do it, from start to finish. Admittedly, I slowed down once college started, but that’s to be expected since I did have to learn Python, and Django, as well as how to parse websites!

So, what does my first app do exactly? Well, its called Lotto Helper IE. It is an app to show the user what the most common lotto numbers are in the Irish lottery since its inception in 1988. I basically parsed a website with all the numbers, put them into an XML file, and from there I counted each number and sorted them in order. The problem with this method is that, in 1992 the Irish Lottery Commission added some new numbers, extending them from 35 to 38. The Bonus ball was also added. A few years later, the numbers were then extended to 42. Now, based on frequency alone, this would lead to inaccuracies, since the draw began in 1988 and some numbers were only added 4-6 years later.
To counter-act this, I’ve made a few tweaks. Firstly, I counted how many draws were missed by each new number. Then I subtracted that from the total amount. I could then work out an accurate percentage and sort the numbers by this.

On top of this, I have also added in support for the Euromillions. Similarly, I parse the numbers, store in XML, and then do some calculations. The result gives me an ordered list of the monst common numbers, and how many times they’ve come out.

On the Irish Lotto website, there is a number checker. I’ve checked my predicted numbers and they seem to be doing well. Over the last month, anyone who had played these numbers would have netted themselves over €30! Not bad for a free app!

Apparently, there is big business in betting shops where people can bet on number pairs to come out in each lotto draw. I’m hoping to extend the app to include a feature like this. In the meantime, I’ve a “stats” screen, which displays the raw data for each number in both the Irish Lotto, and the euromillions. I’ve a few hundred downloads so far, which is cool, ranging from Ireland, the UK, and America to Eastern Europe, China and even Brazil!

Lotto Helper IE available for free from the Android Market

My first personally publish app can be download, for free, from the Android Market here:
Lotto Helper IE

-Eoin

Blackberry CRC32 issues

I’m currently a few weeks into my internship where I’m developing a new blackberry app for a small start-up company. One of the core features of this app is that it can communicate with a remote server. Anyone that knows anything about security will tell you that you simply cannot send these communications in plain text.

So, this week I’ve been working with encrypting strings and sending them to our server. The way we’re handling the encryption/decryption is by using a common method on both client and server side so that messages can be easily decrypted. Pretty common enough. But, in order for the client and server to tell if messages have been tampered with before the arrive at their destination, we also send a CRC of the string that we have encrypted. Once decrypted, we call the CRC method again and can tell if the data is as it should be.

Here’s where the problem arises.

Since the app is cross platform, we have to ensure that the CRC value gotten on a blackberry device matches that gotten on an Android device, not to mention the server will need to be working off the same algorithms. Initially you might think thats no problem because CRCs are universal. Incorrect. It took me a few days but I have realised that the Blackberry implementation of CRC32 is different to the Android and the Apache/PHP implementation. It was frustrating having the Android sending and receiving messages with ease, when my blackberry app was getting errors.

The Solution
Like all good programmers will tell you, there’s no point reinventing the wheel. It felt to me like that was going to be my only solution if I was to ever get this to work, but after spending way too long on this small piece of code, I decided to do what I should’ve done a long time ago….and that is to Google java.util.zip.CRC32 source (and java.util.zip.checksum) and copy and paste the source into a new .java file in my project! Basically the blackberry CRC32 code seems “broken” so why invent the wheel when I can just use the same “working” code that the others use!!!

After I did this, the code worked straight away. I didnt even have to rewrite the code too much because they all have pretty similar methods! Plus, my code is a little bit more “cross-platform” now for when we package all the common stuff into a JAR later.

Getting to grips with Blackberry

I mentioned previously that I’ve taken on an intern position with a company specialising in making child safety/security apps, and I’ve been given the role of developing the Blackberry version of the app. Well, after being in the job a few weeks now, theres some thing I’ve noticed.

The JRE version used to make Blackberry apps is JRE 1.3, which basically means no ArrayLists, no generics (the pointy braces , eg ArrayList) and no StringBuilder. The absence of StringBuilder isnt that big a deal since you can still use StringBuffer. There’s also no template classes, but I think thats more of a Java thing. Coming from a C++ background, I like to have Template classes instead of overloading where possible, but I suppose thats more of a preference, and not really a limitation.

I’ve also noticed that the simulators (not emulators ;)) for blackberry can be very slow and memory intensive. Much like that Android emulators, they take forever to load up, but unlike that Android emulators, you can’t skip the boot animation to slightly speed things up. My laptop has 1GB of RAM, but I’ve ordered another 1GB off eBay so hopefully that speeds things up a bit. From what I’m told, RAM is more important than CPU speed here.

Also, there are 2 IDEs that are commonly used for Blackberry. There’s the Eclipse plugin, and then there’s RIMs own Blackberry JDE, built in Java. Both work fine but the one thing its missing is the ability to filter the console. In Android, you can filter by a tag or app name. In blackberry there is no such option, so your console outputs get lost in a sea of scrolling lines! The best workaround I’ve found is to get a plugin for eclipse, called grep console that highlights lines in a specificed colour based on text matching. So if you output to the console something like: MyApp::Error with database then the plugin can match MyApp:: and highlight it red, or whatever colour you choose.

Anyway, that’s just my initial observations and a newbie to the Blackberry world. Will keep updating here as I find out more.

I got my results!

After a long year of basically learning a brand new (to me) technology, I’ve finally gotten my results back for this project, and my other subjects too.

In my last post, I had finished off the geolocation code on the backend, which was basically all that was left to do on that side. The rest of the work was done on the front-end.

The complete list of features are as follows:

    Get the nearest 3 Dublin Bikes stations, with live info
    Get the nearest 3 bicycle lockup areas in the city
    Get the nearest 3 LUAS stops, with live arrival times
    Add favourite bike/LUAS stations

Now that I list them out like that, it doesnt really seem like there is a lot to the app, but I can assure you, there was a lot of work put into it! Honest!

My intention now is to create an Android equivalent now. Also, I’ve managed to get my hands on the coordinates of every Dublin Bus station in the city, so I might add buses to the app as well, since I can get live info off the Dublin Bus website. Clicking on a bus stop would mean it will display live Bus times, which could come in really handy. I might even release it to the market after that.

OH ye, and in case you’re wondering what result I got for this…..

I got an A!! 😀

I now have my bachelor of science degree, which apparently makes me a scientist. My plan now is to go on and do the honours bachelors degree, and presumably become an “honours scientist”(?).

I’ve also a few projects I want to get done over the summer so I’ll continue with this blog, updating it with what I’ve learned from them.

See ya soon!

« Older posts