Wednesday, May 11, 2011

Logging and Tracking on Android Applications


Tracking what is happening on your program either to inform the user, to debug a program or just to record information of what is occurring on it for later processing is a useful resource for a developer. This becomes important when you are running a Service with no visible interface with the user. Let’s review two ways to do so:

Writing to the Log. Android keeps a log of events. The OS and apps are constantly writing to it and your apps can write too.

One way to take a look at this log is using ADB (Android Debug Bridge) from the command prompt. CD to your SDK folder and look for the abd.exe application currently under the platform-tools folder.

CD  \android-sdk\platform-tools

Now type

Adb logcat

Notice that you have to have an emulator running or a phone plugged.

You should start looking at the live log. Do something on your phone or emulator and you will see that something gets logged.

To write to this log you will use the Log class as this:

Log.i(tag, msg);

Where tag is an identifier to associate the msg with a particular group of items, for instance:

Log.i(“MyAppName-MyClass”, “I’ve reached point XYZ”);

Note that you have to:

import android.util.Log;

Google android.util.Log  for more information.

Displaying a quick message on the screen.  A second way is to use the Toast Class which produces a kind of pop-up temporarily message on top of the current Activity.

To display a message write:

Toast.makeText(this, "Hi There!", Toast.LENGTH_LONG).show();

Note that you have to:

import android.widget.Toast;

For messages that will be part of a deployed solution, use resource strings instead of hardcoded messages:

private static final String TAG = YourClass.class.getSimpleName();
Log.i(TAG, R.string.msgXYZ);

Toast.makeText(this, R.string.msgXYZ, Toast.LENGTH_LONG).show();

1 comment:

  1. it good ...but give me more information about android execution

    ReplyDelete