Archive for category How to
How to Open a URL from Code
Posted by Simon Judge, Freelance Mobile Developer in Android, Coding, How to on April 15th, 2009
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.ACTION_VIEW,
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.
How to Measure Elapsed Time
Posted by Simon Judge, Freelance Mobile Developer in Coding, How to on April 9th, 2009
Many applications have to measure time intervals. Android has lots of SystemClock APIs and initially it might be confusing what to use. To measure elapsed time you can use…
System.nanoTime();
An elapsed time timer just calls this twice and subtracts the end time from the start time.
The boom-mobile source code has a great StopWatch class that you can use as a basis of a timer.
How to Get the Phone IMEI
Posted by Simon Judge, Freelance Mobile Developer in Android, Coding, How to on April 2nd, 2009
Another common requirement for many applications is to get a unique Id you can use to identify the user. Most applications, on all platforms, use the IMEI rather than the telephone number (MSISDN) because the SIM isn’t guaranteed to contain a MSISDN (or the correct MSISDN) - I believe this depends on the specification when the SIM is manufactured.
Under Android, it’s easy to get the IMEI. There’s an example in the OS Source in android.provider.Settings …
String imei = TelephonyManager.getDefault().getDeviceId();
if (TextUtils.isEmpty(imei)) {
return "";
}
The comments in android.provider.Settings explain that if the TelephonyManager isn’t ready then you will get an empty string. In the example above, the code returns an empty string if this happens.
In order to be able to fetch the IMEI you will need to declare the following in your manifest…
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
This might cause your end users to question what your application is actually doing. If you don’t like this then you might like to consider using the system settings Android_ID as described at strazzere.com.
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 Make a Call
Posted by Simon Judge, Freelance Mobile Developer in Android, Coding, How to on March 24th, 2009
One common requirement I see in specifications is to allow the application to make a phone call. The usual way is to use an intent with Intent.ACTION_CALL.
Here’s an example from com.android.phone.PhoneInterfaceManager…
String url = createTelUrl(number);
if (url == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(mApp, PhoneApp.getCallScreenClassName());
mApp.startActivity(intent);
createTelUrl is defined in com.android.phone.PhoneInterfaceManager
Don’t forget to add…
<uses-permission id="android.permission.CALL_PHONE"/>
…to your manifest.
Note also that there’s a reported problem on the G1 (RC30) if you wish to send many DTMF tones.

