Monday, February 09, 2009
Minimal code to perform logging in Google Android
At the top of the file in the imports section add:
import android.util.Log;
And then in your code you add
Log.d(TAG,message);
Where
TAG
and
message
are strings.It is considered good form that the TAG is a constant for the class.
So I put something like this at the top of the class
private static final String TAG = Foo.class.getName();
There are several levels of logging and in level order these are
Log.v(), Log.d(), Log.i(), Log.w()
and
Log.e()
Where
v = verbose
d = debug
i = info
w = warning
e = error
The logging is performed by LogCat which can be accessed via the adb tool that comes with the Android developer kit.
tools/adb logcat
will start logging at the
INFO
level as that is the default.tools/adb logcat *:V
will log everything at
VERBOSE
level.tools/adb logcat Wibble:W
will filter most things and only show those log statements with the
tag
Wibble
at the
WARNING
level.
Labels: google android
Thursday, February 05, 2009
Minimal code to display a Toast message in Google Android
// show the frost pist message using the Toast widget
Toast toast = Toast.makeText(context, "Woo hoo, toast", Toast.LENGTH_LONG);
toast.show();
I find the
context
is normally part of the method signature. Toast.LENGTH_LONG
keeps the message up for a few seconds.There is a
Toast.LENGTH_SHORT
if you don't want the message to hang about for too long.
Labels: google android
Minimal code to retreive a GPS location in Google Android
First up, a GPS location listener.
Somewhere else, probably in the
class GPSLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Activity
you need to tie this listener in. Here is an
onCreate
from a minimal application's
Activity
.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GPSLocationListener gpsLocationListener = new GPSLocationListener();
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
long minTime = 600000;
float minDistance = 10;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsLocationListener);
}
minTime
is a request to the OS about how often to check the GPS location. If
you set it less than 1 minute (60000) then you risk the battery going
flat quickly because the GPS receiver will be on all the time. The
number is just a request. The OS may request a fix more often, it may
check less often.minDistance
is a request to trigger the listener when the device has moved by
this distance.You also need
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
in the applications
AndroidManifest.xml
file.
Labels: google android
Minimal code to send a SMS from Google Android
This code assumes you're in an
Activity
, which is where it gets
this
:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0)
SmsManager smsManager = SmsManager.getDefault();
String receiver = "5556"; // the phone number of the device to send the SMS message to.
String message = "This is the SMS message I want to sending";
smsManager.sendTextMessage(receiver, null, message, pendingIntent, null);
What it requires from
this
is actually just the interface to the
Context
.The
<uses-permission
android:name="android.permission.SEND_SMS"></uses-permission>
is also required your build's
AndroidManifest.xml
file
Labels: google android
Tuesday, February 03, 2009
Telling the android emulator where you are.
telnet
ed into an emulator you can poke in a pair of map co-ordinates to
give the impression the GPS unit has just got a fix.Fun thing to remember is that the latitude and longitude are the wrong way round: you put the longitude first. Additionally use minus signs instead of chars, remembering North and East are positive.
So to fix a position like 55.623131N 11.997169 becomes 11.997169 55.623131
telnet localhost 5554
Trying ::1...
telnet:
connect to address ::1: Connection refused
Trying fe80::1...
telnet:
connect to address fe80::1: Connection refused
Trying
127.0.0.1...
Connected to localhost.
Escape character is
'^]'.
Android Console: type 'help' for a list of commands
OK
geo
fix 11.997169 55.623131
OK
Note it uses proper decimal, such that 55° 35' 10" becomes 55.58611111.
For extra fun the
geo
command can even take a real
nmea
message
geo nmea
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
OK
And if you tell the mapping application in the emulator to 'My Location', off the map will fly.
Labels: google android
Sending an SMS into a Google Android emulator.
What you do is
telnet
into the emulator you wish to poke the message into, and then tell it
that it's just got a message from some place. In this example below,
it is one of the other emulators.
telnet localhost 5554
Trying ::1...
telnet:
connect to address ::1: Connection refused
Trying fe80::1...
telnet:
connect to address fe80::1: Connection refused
Trying
127.0.0.1...
Connected to localhost.
Escape character is
'^]'.
Android Console: type 'help' for a list of commands
OK
sms
send 5556 Hello!
OK
If you've something listening out for SMS messages, it will now trigger and the two emulators can chat, if that is what you want.
Labels: google android
Monday, February 02, 2009
Running multiple Android emulators
It then kills any adb server that may be running, starts a new one, and lists the running emulators.
#! /bin/bash
xterm -geometry
132x100+100+100 -sb -e bash -c
'/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L
-data droids/droid-01.img -verbose -logcat "out"' &
xterm
-geometry 132x100+200+200 -sb -e bash -c
'/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L
-data droids/droid-02.img -verbose -logcat "out"' &
xterm
-geometry 132x100+300+300 -sb -e bash -c
'/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L
-data droids/droid-03.img -verbose -logcat "out"' &
sleep 30s
/Library/android-sdk-mac_x86-1.0_r2/tools/adb
kill-server
/Library/android-sdk-mac_x86-1.0_r2/tools/adb
start-server
/Library/android-sdk-mac_x86-1.0_r2/tools/adb
devices
-skin HVGA-L : 480x320, landscape, puts the emulator of the screen sideways
There are other options for skin:
HVGA-P
is default.
QVGA-L
gives a 320x240, landscape and
QVGA-P
is 240x320, portrait-data droids/droid-01.img : a different number for each running emulator ensures they save their data to their own area.
-verbose : lots of logging information. Can slow the emulator down to the point where they give error messages because they run things too slow...
-logcat : the first rule of logcat is: no one talks about logcat.
Labels: google android
Getting and building Google Android from the source code on Mac OS X
Install MacPorts, available from http://svn.macports.org/repository/macports/downloads/MacPorts-1.7.0/
Open up a terminal, check /opt/local/bin is at the start of $PATH, and run
sudo port selfupdateHopefully it'll say something like:
Password:Get these packages from port, with the following command:
MacPorts base version 1.700 installed
Downloaded MacPorts base version 1.700
The MacPorts installation is not outdated so it was not updated
POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupgGo make a cup of coffee, or two, as several minutes will pass, depending on your machine and download speeds, and several terminal screens full of build comments will scroll slowly by.
Installing repo
Go back to your home directory:
cd ~If you don't already have one make a bin folder:
mkdir binMake sure it's on the path:
export PATH=/Users/threaded/bin:$PATHDownload the repo script:
echo $PATH
curl http://android.git.kernel.org/repo >~/bin/repoLook to see if it is executable:
ls -al ~/bin/repoMost probably it isn't so:
chmod a+x ~/bin/repoTo build the Android files you're recommended to use a case-sensitive Journaled HFS+. If you don't want to go creating a partition, formatting it, all the other ball-n-chain, you can, on the Mac, create a disk image. It is recommend to be 8 GB, but more won't harm.
What I did:
- launch /Applications/Utilities/Disk Utility
- select "New Image"
- Save As: mydroid (in my home directory)
- Volume Name: mydroid
- Volume Size: 16 GB (had to do this a couple of times before
it 'took')
- Volume Format: Mac OS extended (Case-sensitive, Journaled)
- Encryption: none
- Partitions: single partition - Apple Partition
- Image Format: read/write disk image
cd /Volumes/mydroidRun repo init to get the files:
repo init -u git://android.git.kernel.org/platform/manifest.gitIf you get a permission denied, then you've forgotten to do the chmod to repo mentioned above, like I did. ;-)
After a little huff-n-puff, it'll ask for a name and email. The name should be real, as it'll be used for attributions, and the email address needs to be to a Google account. This doesn't have to be a Gmail address, but that's easiest.
Now you can get the files:
repo syncAgain this is long-winded, so there's time for yet another coffee.
Now, finally you can:
makeIf you get:
build/core/main.mk:64: ************************************************************That'll be because you are not where you think you are, but should rather be inside a Case-Sensitive, Journaled file system. ;-)
build/core/main.mk:65: You are building on a case-insensitive filesystem.
build/core/main.mk:66: Please move your source tree to a case-sensitive filesystem.
build/core/main.mk:67: ************************************************************
build/core/main.mk:68: *** Case-insensitive filesystems not supported. Stop.
Another chance for several more cups of coffee. Lots of warning messages fly by on the terminal that don't really mean that much.
You can see if it works by dragging the newly built emulator to a terminal window:
Volumes/mydroid/out/host/darwin-x86/bin/emulator
which should illicit the response:
emulator: ERROR: You did not specify a virtual machine name, and the system
directory could not be found.
If you are an Android SDK user, please use '@' or '-vm '
to start a given virtual machine (see -help-vm for details).
Otherwise, follow the instructions in -help-disk-images to start the emulator
Labels: google android
Thursday, October 09, 2008
Gotchas with Google Android Development
-scale
setting for the emulator, which can vary between 0.1 and 3, so a
value of around 0.6 makes the Android Emulator fit to a 600 pixel
depth screen.The Android Emulator can take a fair time to start up, up to 20 minutes on a PC, (but usually only 10 seconds or so on a mac pro,) so give it a minute or so with A N D R O I D displayed before you give up and look at what might be wrong. (It'll generally tell you on the command line if there really is something wrong, and that you're not being a tad hasty to kill the process.)
On really slow PCs you may get a message
Sorry!
Application Messaging (in
process
com.android.mms) is
not responding.
Which means, if you get it frequently, your machine is way too slow: you might like to try and install Linux or go out and buy something faster like a Mac.
Labels: google android
Getting started with Google Android
Download the Android SDK and get the emulator to work.
Android SDK: http://code.google.com/android/download_list.html
When you've unpacked it and tried to run the emulator you'll maybe get some failure mentioning
-datadir
.I generally open up a terminal and create the directory it wants.
mkdir .android
cd .android/
mkdir SDK-1.0
or you could just put it on the command line
emulator -datadir ~/temp
Then you want the Eclipse IDE:
Eclipse IDE for Java EE Developers (i.e. the 163 mb one)
Fire up Eclipse and now install the Android Development Tools.
In Eclipse:
Help
Software Updates...
Available Software
Add Site...
https://dl-ssl.google.com/android/eclipse/
etc. etc.I think it's best to restart Eclipse when you get to the end.
And there you have it, a Google Android development environment.
Labels: google android
Subscribe to Posts [Atom]