Archive for category Getting Started
Fixing Speaker Buzz and Emulator Crash
Posted by Simon Judge, Freelance Mobile Developer in Coding, Eclipse, Getting Started, How to, My Freeware, Tools on February 22nd, 2010
Unfortunately, the Android emulator doesn’t always work well with desktop sound. This can cause the desktop speakers to start buzzing just after boot. The emulator also crashes and it’s not possible to close it.
The solution is to turn off sound support in the emulator. You can do this by adding -noaudio to the emulator command line. Open Run Configurations and select the Target tab. Add -noaudio to the ‘Additional Emulator Command Line Options’…
Obviously, you won’t be able to hear or test sound if you use this option.
Deep Inside Android
Posted by Simon Judge, Freelance Mobile Developer in Android, Events, Getting Started, Linux, My Freeware on March 26th, 2009
I came across some useful slides recently. They are entitled ‘Deep Inside Android‘ from a presentation by Esmertec, an OHA member, at the OpenExpo 2008.

The presentation is useful for people who want to learn a bit more about Android Linux internals. The later slides also cover Android programming essentials.
How to Disable the Keyguard
Posted by Simon Judge, Freelance Mobile Developer in Android, Android Market, Coding, Events, Getting Started, How to on March 25th, 2009
There are some types of application where you need to programatically disable the keyguard. It’s also sometimes useful to temporarily do this when you are presenting a demo. com.android.alarmclock.AlarmAlert in the Android OS source code provides and example how to do this…
private synchronized void enableKeyguard() {
if (mKeyguardLock != null) {
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
}
private synchronized void disableKeyguard() {
if (mKeyguardLock == null) {
mKeyguardLock = mKeyguardManager.newKeyguardLock(Log.LOGTAG);
mKeyguardLock.disableKeyguard();
}
}
where
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
I have also come across a great class, ManageKeyguard in the source of SMSPopup. This provides an example how to disable the keyguard, show your activity and re-enable the keyguard without letting someone get past the keylock to other activities. There’s also more about this discussed on the Google groups.
How to Start on Boot
Posted by Simon Judge, Freelance Mobile Developer in Android, Coding, Getting Started, How to on March 20th, 2009
In porting S60 and Windows Mobile applications to Android, I have found a common requirement is to start a service on boot. This is used for applications that sit in background and listen and react to phone events. In this post I give a summary how to do this on Android and some tips when not to use this method.
Inherit your class from BroadcastReceiver and provide an onReceive(). Instantiate an Intent and use the call Context.startService(). The Android OS source for IM com.android.im.receiver.ImServiceAutoStarter provides an example how to do this…
public class ImServiceAutoStarter extends BroadcastReceiver { static final String TAG = "ImServiceAutoStarter"; @Override public void onReceive(Context context, Intent intent) { // Received intent only when the system boot is completed Log.d(TAG, "onReceiveIntent"); String selection = Im.Account.KEEP_SIGNED_IN + "=1 AND " + Im.Account.ACTIVE + "=1"; Cursor cursor = context.getContentResolver().query(Im.Account.CONTENT_URI, new String[]{Im.Account._ID}, selection, null, null); if (cursor != null) { if (cursor.getCount() > 0) { Log.d(TAG, "start service"); Intent serviceIntent = new Intent(); serviceIntent.setComponent(ImServiceConstants.IM_SERVICE_COMPONENT); serviceIntent.putExtra(ImServiceConstants.EXTRA_CHECK_AUTO_LOGIN, true); context.startService(serviceIntent); } cursor.close(); } }
You must also have something like the following in your manifest…
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.ImServiceAutoStarter"
android:process="android.process.im">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The ImServiceConstants.IM_SERVICE_COMPONENT is defined as com.android.im.service.RemoteImService in…
public class ImServiceConstants {
/**
* RemoteImService name, used for start or stop the IM service.
*/
public static final ComponentName IM_SERVICE_COMPONENT = new ComponentName(
"com.android.im",
"com.android.im.service.RemoteImService");
The service itself is written by inheriting from Service…
public class RemoteImService extends Service {
Provide onCreate() and onDestroy() methods that are used when the service is started and stopped.
You must also define the service it its manifest…
<service android:name=".service.RemoteImService" android:process="android.process.im" android:exported="true" android:permission="com.android.im.permission.IM_SERVICE"> <intent-filter> <action android:name="com.android.im.IRemoteImService" /> <action android:name="com.android.im.SERVICE" /> </intent-filter> </service>
As I mentioned, the above is suitable for long running applications that have to sit in the background and listen and react to phone events. For ‘real’ services that provide functionality for zero or more activities, it’s better to use bindService() rather than startService() because the application manager is free to destroy the service, to save resources, when it isn’t being used. There’s a great example on bindService() at My life with Android.
An Android service can actually be both a background application and provide functionality for activities. In this case there’s only one service instance.
Finding Sample Code
Posted by Simon Judge, Freelance Mobile Developer in Android, Coding, Getting Started on March 18th, 2009
When coding for Android, one problem is finding working example code. This is because the Android APIs have changed over time and what some 3rd parties may have documented last year won’t necessarily work with the new SDK.
Many of the Android forums and blogs seem to have lost their momentum and have very little new material since early to mid 2008. This is because Google took a long time to release the Android 1.0 SDK.
Another problem is that there are fewer official source code samples compared with say S60, iPhone, Java ME and Windows Mobile.
On the positive side, it’s actually very easy to code for Android. The only missing feature is still a resource editor. You have to code the UI yourself using XML. Alternatively try the 3rd party DroidDraw. It will get you going and before long you will find you will be able to code resources by hand. Alternatively, with just a little more effort, you can create controls dynamically in the code.
My experience is that Java ME code doesn’t port that easily. There are too many different concepts and classes. However, non mobile Java, (desktop and server) ports more easily and many open source utility libraries/classes work well. However, beware of relying on these as there’s often an in-built Android equivalent you can use.
The lack of examples can also be solved by viewing the Android source code that includes many of the in-built applications. The Android web pages fool you into thinking you need Linux and to install lots of convoluted tools. However, it’s possible to download compressed snapshots of projects…
Go to…
http://git.source.android.com/
Apps have path platform/packages/apps/xxxxxxx.git
Click on git and select snapshot to get .tar.gz
You can now view these on a Windows PC with an application such as 7-Zip.
There are projects for the alarm clock, browser, calculator, calendar, camera (oh, somebody please work on that G1 shutter lag!), contacts, email, Google search, HTML Viewer, IM, MMS, music, the phone, sound recorder, sync and the voice Dialer.
If you are feeling adventurous you can also download the Dalvik implementation. As with most JVMs, it is itself written in Java! Only a small proportion is actually c.

