Friday 18 February 2011

Start the youtube player in a simple way

Don't spend time on reinventing the wheel. If your app needs to play a youtube video, just start the youtube player like this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));

Thursday 3 February 2011

Reloading a WebView every x seconds

If you load a page using WebView and want to reload that page to show the latest updates etc. java has an easy solution for that:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(new Runnable() { @Override public void run() { Shout.loadUrl("http://www.bengaard.com/OB/shoutstart.php?konto="+konto); } }, 10, 20, TimeUnit.SECONDS);


So this piece of code simply reloads the page every 20 seconds with a 20 second initial delay which is very ideal for updating my shoutbox. Of course, you can change the WebView with whatever code you need to execute between fixed intervals.

Tuesday 1 February 2011

Login using your Google account

Since 99,99% of all android users have a gmail account it can sometimes be useful to use that email address as a login or username for the users of your app. Here's how to do it:

protected AccountManager accountManager; accountManager = AccountManager.get(getApplicationContext()); Account[] accounts = accountManager.getAccountsByType("com.google");
The variable accounts will now hold an array of all the google accounts ever used on the phone. So to get the first account/email address registered on the phone try something like

konto = accounts[0].name;
You can then use the account name/email address for login, shoutbox name, etc. etc.

Monday 31 January 2011

How to sign your app with keytool

When your app is ready for Market you have to sign it using keytool.exe. It means that your app will carry a secret code which is supposed to certify that you're the original creator of the app. So, let's suppose you have finished developing your app. Now what?

  1. Find the location of keytool.exe on your harddrive. Keytool is part of your java installation. I installed java on my C: drive. So using a cmd prompt on windose I change the folder to this:

  2. Next up we're going to create a keystore. I guess this must be a kind of store with keys for your app ;o) Here I create my own keystore using my company name, 'bengaard'. Change that to a name you like, follow the steps and you'll create the keystore:

  3. When you export you app from Eclipse,





















  4. you'll then get a chance to write the password you provided when you created the keystore:





















  5. And that's pretty much it! You can now upload you app to Market.

Sunday 30 January 2011

The Android Manifest file

So, time for the first hint when writing your android app using the android sdk. Today we'll talk about the AndroidManifest.xml file. You see, every app must contain an AndroidManifest.xml file in its root directory. Fortunately for you an android project is always born with this file and most often you don't have to check it. In eclipse we find it here under the project OB:

When should you care about the manifest file?

  1. When your app need to utilize an internet connection
  2. If your app closes unexpectedly during startup and you don't see any other errors in your code (AND you've spend hours of debugging hehe)

Lets take a look at the manifest file for the OB-app'en app:

<?xml version="1.0" encoding="ISO-8859-1"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bengaard" android:versionCode="3" android:versionName="2.0"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.SET_ORIENTATION"></uses-permission> <uses-permission android:name="android.permission.ACCOUNT_MANAGER"></uses-permission> <uses-permission android:name="android.permission.MANAGE_ACCOUNT"></uses-permission> <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> <uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission> <uses-sdk android:minSdkVersion="3"></uses-sdk> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".OB" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


Now, the segments that you need to pay attention to is line 4 about the internet permission. If your app uses an internet connection in any way your manifest file needs to have this permission. Otherwise it just can't use a connection and nothing will happen with your app, no error, no nothing!

Also, while developing the app it would shut down numerous times unexpectedly. I found out that the reason might be that permissions were missing from the manifest file. So I wrote some more permissions found on http://developer.android.com/guide/topics/manifest/manifest-intro.html just to be sure and that seemed to help avoid the app to shut down all the time.

When publishing your app to market notice that your manifest file has to have a package name, versionCode and versionName included. If you intend to publish your app as an update to a previous app, you need to use the same package name, a different versionCode while the versionName can be whatever you find cool. Of course, notice that if you write unnecessary permission statements your app might ask for more rights from the user than needed, which isn't a good thing. The android:minSdkVersion="3" indicates that the app runs on Android versions from 1.5 and up.

From App Inventor to Android sdk

Oh wow, did I have a great time exploring Google's App Inventor. That's simply an amazing tool for the noobish programmer. As simple and intuitive as it is it still makes you do some pretty nasty stuff. Like the best programming languages I had a feeling that the only real boundary is yourself. And all those years playing with lego finally paid off :o) After publishing my first app created with AI to the market (it's not there anymore, sorry) I kind of realized I had to move on. The #1 reason was speed. AI is simply too slow running on the phone. My app would take seconds to fetch JSON-encoded data off a web page. The Android sdk lets me use threads to get the data with virtually no delay. The #2 reason was bling. The sdk along with java simply lets you do much more sophisticated stuff that AI never will be able to. Anyway, maybe the best part about AI was that because of it's nature you don't spend time on debugging code. Nice!

OB-app'en v2.0 released!

Release notes:
  • Shake-a-Player  -  shake your favorite android phone to pick a random player
  • Rss support for your OB news feeds
  • Complete list of top scorers
  • Automatic login with your google account
  • Improved shout box
  • Change of sdk from App Inventor to Android sdk
and lot's of more to come! :o)