Android licensing simple example

SDK Version: 

Android Market Licensing is a network-based service that lets an application on an Android-powered device query a trusted licensing server, to determine whether the application is licensed to the current device user. After receiving the server response, the application can then allow or disallow further use of the application as needed. In the service, the role of the licensing server is to provide the license status for the current user; the application itself is responsible for querying the server and conditionally granting access to the application.

You can find the licensing library and a cool sample application in your android-sdk folder after you download it with Android SDK and AVD manager. This tool located under Windows menu in Eclipse.

Simple UDP communication example

SDK Version: 

Today I’m going to show you how to create a really easy and simple UDP message sender and receiver.

Udp communication time diagram.

The User Datagram Protocol (UDP) is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without requiring prior communications to set up special transmission channels or data paths. /wiki/

First of all let’s see the server side:

  1. String messageStr="Hello Android!";
  2. int server_port = 12345;
  3. InetAddress local = InetAddress.getByName("");
  4. int msg_length=messageStr.length();
  5. byte[] message = messageStr.getBytes();

How to update custom listview images simply

SDK Version: 
So in this tutorial I'm going to show you how to refresh imageviews' contents periodically (let say by Handlers if you download the picture from web).
I had painful 2 days figuring out what's a good solution here, I tried to give IDs to imageviews and that sort of sick things, but believe me, it wasn't worth it. The solution is so simply that I hardly can believe.
What we're lookign for here is instead of create new Adapters and HashMaps (which contains ListView data), we just update it's values, and Android will do the trick for us.
The most important thing is DO NOT AT ANY CIRCUMSTANCES CREATE A NEW ADAPTER (or a new data source that holds the Adapter's data).
There's an exception of course, you obviously have to create a new Adapter in OnCreate() { }.

Let me show you how it works:

How to set an image as wallpaper on different api levels

SDK Version: 

Pretty easy! :)
Use the context’s setWallpaper method.

  1. getApplicationContext().setWallpaper(bitmap)

Don’t forget to add the SET_WALLPAPER permission to your AndroidManifest.xml!

  1. <uses-permission android:name="android.permission.SET_WALLPAPER" />

Differences between api versions
On Android 1.5 the wallpaper looks cool, doesn’t it? :) The backgroud fills the screen.

But on 1.6 and higher android strechs the wallpaper like this:

It’s not bad if you use high resolution wallpaper, but the low res wallpapers look crappy.

Leaving an Android application

SDK Version: 
One of the strange things with android is that there is no "the user has closed the application" event. Applications activities often just go to the background, and the android system may kill them whenever it wants, even when another activity of the application is still running in the foreground.

You can not be sure that activities you started and sent to the background are sill there storing their state, and if you are just an android user, you can not be sure that applications that you left simply by pressing back button are completely destroyed.

To make it more complex, using intents it is even possible to start an activity of another application.

How to make a phone call from your application

SDK Version: 

To enable your application to initiate a phone call, you must set permissions in the manifest file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.       package="com.bubudsadasdas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".phonecalls"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="3" />
  17. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  18. </manifest>

Now create a new activity with a call method. It should look like this:

Android unit testing

SDK Version: 

For unit testing you can use the built in JUnit framework just like in "standard" java applications.

A unit test is to test some low-level part - usually a single class - of a project, working individually.

The only trick is to extend AndroidTestCase in your test case instead of TestCase some Android specific methods, like getContext() which is often required by android functions.

Lets see a very simple example using eclipse:

- Create a new project to test, create a new class in it, with a simple method that , like this:

  1. public class ClassToTest{
  3.    public int add(int arg1,int arg2){
  4.            return arg1+arg2;
  5.    }
  7. }

- Create a new test project: File/new/Other.../Android/Android Test Project

new Android Test Project

Add the previously created project as the test target, the other fields can be left as they are auto filled.

Date handling in Android development

SDK Version: 

This unusual topic came around quite a few times in the last couple of days, first with our own rss parser, and today with android-xmlrpc.
In our rss parser, we wanted to have as much flexibility as possible, so we could use many types of localized rss pages, that have different date formats.

Is this the first run?

SDK Version: 

Ever wanted to have a different flow of actions on the second or third run of your app? I had that many times, in almost every project that I haver worked on. So here is a little snippet, that you can use to store, and check the fact, if this is the first run of your app. You can modify it easily, to suit your needs.

How to have a default database

SDK Version: 

If you want to include a database with initial data in your apk, you have to insert the database file into the projects assets folder, then programmatically check if the database, and if it does not exists copy the one from the assets.

You will need a function to check if the database exists, fox example:

  1. private static final String DATABASE_NAME = "testdatabase";
  2. private static final String DB_PATH = "/data/data/"+mContext.getPackageName()+"/databases/";
  4. private boolean isDataBaseExist() {
  5.         File dbFile = new File(DB_PATH+DATABASE_NAME);
  6.         return dbFile.exists();
  7. }

If the check shows that the the database does not exist, a function like this will copy it:

  1. private void copyDataBase() throws IOException {
  2.         // Open your local db as the input stream
  3.         InputStream myInput = mContext.getAssets().open("databases/"+DATABASE_NAME);
  4.         // Path to the just created empty db
  5.         String outFileName = DB_PATH + DATABASE_NAME;

Syndicate content