2011年8月8日星期一

native application development for Android

/android



This page is dedicated to native application development for Android (primarily using C/C++).


Required software

Optional software

Contents
  1. Hello, world
  2. Reading the keypad
  3. Building SDL
  4. Dynamic linking
  5. Using OpenGL ES
  6. Debugging
  7. Bitmap color reduction and GIF encoding
  8. A practical example


Hello, world

Let's start off with the trivial "Hello, world!" example. There's nothing special about doing this on Android, other than perhaps how you compile and deploy the program.

hello.c
#include   int main(int argc, char **argv) { 	printf("Hello, world!\n"); 	return 0; } 
Compile with arm-none-linux-gnueabi-gcc -static -o hello hello.c

After starting the Android device/emulator you need to push your program to the file system using the adbtool located in the tools directory of the Android SDK.
From the command line, do:

adb push hello /data/misc/hello

You can push the program to some other directory if you prefer. If adb complains about the file system being read-only (or "no such file or directory"), try remounting it in read-write mode. For example, if you wanted to be able to push the program to somewhere in /system:

adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /system

The next thing to do is to make sure that your program has the right attributes to be executed:

adb shell chmod 777 /data/misc/hello

Then you can execute your program through the shell:

adb shell /data/misc/hello


Reading the keypad

Getting input from the shell can be done simply by reading from stdin. It might be more interesting, however, to get input from the device's keypad, and this requires some extra steps. The keypad is located at/dev/input/event0, so what you want to do is read from that device. Below I show one way of doing this:

andkeys.h
#ifndef ANDKEYS_H #define ANDKEYS_H  #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>  typedef struct {         struct timeval time;         unsigned short type;         unsigned short code;         unsigned int value; } input_event;   // Opens the Android keypad device for reading void andkeys_open();   // Polls the Android keypad device for new data. Returns 0 if there was no new data. // Otherwise non-zero is returned and the input_event struct pointed to by 'ie' is // filled with the latest data. int andkeys_get(input_event *ie);   

没有评论: