Archive for category How to

Fixing Speaker Buzz and Emulator Crash

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’…

targetnoaudioObviously, you won’t be able to hear or test sound if you use this option.

How to Send an Email

Another common requirement is to send an email from your application. At first, this looks easy. You use an ACTION_SEND Intent as demonstrated by the example on OpenIntents

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "email text");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Title:"));

However, this sends the email via the Android mail app in the phone, prompting you which email app if you have more than one configured. So, how do you send an email silently? It turns out that there’s no official way to send an email directly (via SMTP). The Google rationale is that, for security reasons, the user needs to know (and confirm) that the application is sending an email on their behalf.

However, what if you still want to send silent emails? One option is to port a 100% Java SMTP library. You might try JavaMail. There’s a .jar file on the Google groups but I wouldn’t use it if I were you as it doesn’t respect the original source code license and you don’t really know what you are including.

There’s another attempt at putting together bits and pieces with more details how to go about this yourself but it’s really lots of hacking which most people might like to avoid. Someone else has also claimed to port gnu inetlib.

If you want a cleaner way of sending via SMTP, without polluting your application with lots of unwanted classes, then one solution might be to get your server to do it via a simple php script and send up the information via a http GET or POST.

How to Detect Call State

Some applications need to do clever things whenever there’s an incoming or outgoing call. There’s a great example of this in the source code for ‘five’ (an app providing remote access to your PC’s music collection) where the music PlaylistService listens for incoming calls so it can temporarily pause playback…

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

and…

private PhoneStateListener mPhoneListener = new PhoneStateListener()
{
        public void onCallStateChanged(int state, String incomingNumber)
        {
                try {
                        switch (state)
                        {
                        case TelephonyManager.CALL_STATE_RINGING:
...
                                break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
...
                                break;
                        case TelephonyManager.CALL_STATE_IDLE:
...
                                break;
                        default:
                                Log.d(TAG, "Unknown phone state=" + state);
                        }
                } catch (RemoteException e) {}
        }
};

In many applications, the next stage is to do something based on an incoming telephone number. The number is given by the String incomingNumber parameter shown above.

Also don’t forget the following in your manifest…

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

How to do Text to Speech

TTS 1.4 has very recently become available on the Android Market. TTS has been created by Charles Chen, Software Engineering Team and T.V. Raman, Research Scientist at Google.

You can use the TTS library in your own applications. There are also some hints and tips how to use TTS in your application.

How to Open a URL from Code

Many apps end up opening a web page at some point. This is often useful to allow changeable information to shown, such as a help screen, that can be maintained on a web server in light of questions from users.

I have come across three ways to do this. The first is to create a android.webkit.WebView and call loadUrl() . You can see this being used in com.android.browser.BrowserActivity where selecting home causes the URL, set up in your settings, to be shown…

            case R.id.homepage_menu_id:
                TabControl.Tab current = mTabControl.getCurrentTab();
                if (current != null) {
                    dismissSubWindow(current);
                    current.getWebView().loadUrl(mSettings.getHomePage());
                }
                break;

The above technique is flexible in that you can even use it to call some JavaScript instead.

But what if the server requires you to use a http POST rather than GET? You can use the commons http client and loadDataWithBaseURL as shown on the Google groups.

A second way to show a URL, but in the built-in web browser rather than within your application, is to use an intent…

Intent myIntent = new Intent(Intent.VIEW_ACTION,
   ContentURI.create("http://www.myurl.com"));
startActivity(myIntent);

Finally,you can also create a TextView with the property android:autoLink=”web”. When the user selects a link in the TextView it will open in the built-in browser application.