Monday, February 09, 2009
Minimal code to perform logging in Google Android
To produce log message in Google Android is really quite easy.
At the top of the file in the imports section add:
And then in your code you add
Where
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
There are several levels of logging and in level order these are
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.
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
Toast is a widget to display an informational message to a user whilst they may well be doing something else. I find it useful for debugging on a real device to tell me something internal to the application has been fired. Afterwards I can just strip such things out of the build.
I find the
There is a
// 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
Here I describe the minimal code to retrieve a GPS location in the Google Android OS.
First up, a GPS location listener.
You also need
First up, a GPS location listener.
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 } }Somewhere else, probably in the
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
A minimal code for sending a SMS message from the Google Android OS.
This code assumes you're in an
What it requires from
The
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
fileLabels: google android
Wednesday, February 04, 2009
Hønsekødsuppe
Hønsekødsuppe til 4 personer
En høne på en og en halv kilo, eller halvanden kilo passer bedre.
En spiseske salt
Cirka to liter vand
To eller tre gulerødder
Et kvart selleri
En persillerod
En porre
Et løg
Fire hele nelliker
Stik nellikerne ind i løget.
Tag bladerne fra porren og brug dem til suppevisk
Skær grøntsager i tern.
Kom alt i gryden, untagen kyllingen.
Vær sikker på at hønen er død, en stor hammer kan hjælpe dig her.
Pluk hønen, og fjern indvoldene.
Kom nu kyllingen i gryden.
Kog i to timer ved svag varme.
Omrøres med jævne mellem rum.
Salt og peber efter ønske.
Server og spis op, uden hyl og skrig!
Avis artikel
En høne på en og en halv kilo, eller halvanden kilo passer bedre.
En spiseske salt
Cirka to liter vand
To eller tre gulerødder
Et kvart selleri
En persillerod
En porre
Et løg
Fire hele nelliker
Stik nellikerne ind i løget.
Tag bladerne fra porren og brug dem til suppevisk
Skær grøntsager i tern.
Kom alt i gryden, untagen kyllingen.
Vær sikker på at hønen er død, en stor hammer kan hjælpe dig her.
Pluk hønen, og fjern indvoldene.
Kom nu kyllingen i gryden.
Kog i to timer ved svag varme.
Omrøres med jævne mellem rum.
Salt og peber efter ønske.
Server og spis op, uden hyl og skrig!
Avis artikel
Tuesday, February 03, 2009
Telling the android emulator where you are.
Once
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
Note it uses proper decimal, such that 55° 35' 10" becomes 55.58611111.
For extra fun the
And if you tell the mapping application in the emulator to 'My Location', off the map will fly.
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.
It appears that the ability to send SMS from one android emulator to the other via the SMS application is not currently working, but luckily you can send an SMS into one emulator as if it came from another...
What you do is
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.
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
A little script to fire up multiple Google Android emulators that have their own image, i.e. can save their data separately.
It then kills any adb server that may be running, starts a new one, and lists the running emulators.
-skin HVGA-L : 480x320, landscape, puts the emulator of the screen sideways
There are other options for skin:
-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.
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 XCode version 3.0, available from http://developer.apple.com/technology/tools.html.
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
Installing repo
Go back to your home directory:
What 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:
Now, finally you can:
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:
which should illicit the response:
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
Subscribe to Posts [Atom]