Sensor Fun: Location Based Services and GPS for Android

The Android SDK offers a nice API to receive information about available providers for location based services and get the current location and coordinates. In this short tutorial we’re going to build a small activity that displays a list of available location providers and shows the current position using GPS services. Example Application Create a new Android Project using ADT and your IDE with a package named com.hascode.android.location_app Add the permissions needed to the AndroidManifest.xml – it should look like this <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hascode.android.location_app" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> </manifest> ...

May 30, 2010 · 4 min · 731 words · Micha Kops

Android Gestures

Since version 1.6 Android offers a library for recognition and handling of new gestures using a touch display. With the gesture builder it is possible to capture new gestures in the emulator. Gestures can be integrated into an activity like that. GestureLibrary gLib = GestureLibraries.fromRawResource(this, R.raw.spells); if (!gLib.load()) { finish(); } Furthermore we need a gesture overlay in the UI: <android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" /> In the next step we implement an event handler for the gesture (Source: http://developer.android.com/resources/articles/gestures.html): GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); gestures.addOnGesturePerformedListener(this); public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { ArrayList<prediction> predictions = mLibrary.recognize(gesture); if (predictions.size() > 0) { Prediction prediction = predictions.get(0); if (prediction.score > 1.0) { Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show(); } } } ...

May 21, 2010 · 1 min · 197 words · Micha Kops

Creating a simple Gesture App with Android

The integration of gestures into your android app adds some nice functionality and is made very easy using Google’s GestureBuilder application and the integrated GestureLibrary and Gesture Overlay API – so let’s build a sample app. If you need some basic information regarding gestures on android first – take a look at this article. Creating a gesture library First you need to define the gestures that should be captured in the application later. For this reason there’s the GestureBuilder delivered with the Android SDK. You can find the app in the samples directory of your android sdk – e.g. <installation-directory>/android-sdk-linux_86/platforms/android-2.1/samples/GestureBuilder. ...

May 14, 2010 · 3 min · 635 words · Micha Kops

Playing around with QR Codes

Sometimes QR codes are a nice way to distribute information like calendar events, contact information, e-mail, geo-locations or internet addresses. In the following article we’re going to encode information to QR code images using the ZXing library and afterwards decode information from a given QR code. Finally we’re taking a look on online QR code generators and how to integrate the ZXing library in a Maven project. The ZXing Library Download the ZXing Libraries from http://code.google.com/p/zxing/downloads/list – the file name is ZXing-<version>.zip Unpack the downloaded archive somewhere Change to the extracted directory and run ant. If you don’t have JavaME installed – and you don’t have to for the samples below – run ant buildwithoutj2me – that will do the job Having compiled the libraries you’re now free to include the core.jar from zxing-<version>/core/ and the javase.jar from zxing-<version>/javase as dependency in your project ...

May 11, 2010 · 5 min · 1041 words · Micha Kops

Sensor Fun: Creating a simple audio recorder/player

Sound recording and playback is really simple using the MediaRecorder and MediaPlayer classes .. see the example below .. Sample App First some layout .. a button to start/stop recording and a button to play the recorded stuff (main.xml): <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/output" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <Button android:text="@+string/record" android:id="@+id/btRecord" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="@+string/play" android:id="@+id/btPlay" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> Adjusting the externalized strings in the strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Soundrecorder Tutorial</string> <string name="record">Record!</string> <string name="play">Play</string> </resources> ...

May 2, 2010 · 4 min · 766 words · Micha Kops

Sensor Fun: Using the accelerometer on Android

Here is an example on how to use the accelerometer in your Android application. If you want to simulate the acceleration on the emulator I highly recommend the Sensor Simulator on the OpenIntents website. The following example app displays the coordinates received by the sensor. The Acceleration App The activity – AccelerationActivity.java package com.hascode.android; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class AccellerationActivity extends Activity { private TextView result; private SensorManager sensorManager; private Sensor sensor; private float x, y, z; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); result = (TextView) findViewById(R.id.result); result.setText("No result yet"); } private void refreshDisplay() { String output = String .format("x is: %f / y is: %f / z is: %f", x, y, z); result.setText(output); } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(accelerationListener, sensor, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { sensorManager.unregisterListener(accelerationListener); super.onStop(); } private SensorEventListener accelerationListener = new SensorEventListener() { @Override public void onAccuracyChanged(Sensor sensor, int acc) { } @Override public void onSensorChanged(SensorEvent event) { x = event.values[0]; y = event.values[1]; z = event.values[2]; refreshDisplay(); } }; } ...

April 27, 2010 · 2 min · 280 words · Micha Kops

First steps on Android: Creating a simple Todo App

In this tutorial we are going to build a simple todo app that is able to store simple todos in a database. The user is able to add new todos or delete old ones by clicking on a todo. For this tutorial we won’t use maven to keep it simple – if maven integration is desired – take a look at this tutorial. Steps Create a new android project using the Android SDK and your IDE Create some packages com.hascode.android.activity and com.hascode.android.persistence Create the layout in res/layout/main.xml – the main elements: a listview for the todos-list, a textbox and a button to enter new data. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/widget31" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TableRow android:id="@+id/row" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/tasklist" android:layout_alignParentLeft="true" > <EditText android:id="@+id/etNewTask" android:layout_width="200px" android:layout_height="wrap_content" android:text="" android:textSize="18sp" > </EditText> <Button android:id="@+id/btNewTask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+string/add_button_name" > </Button> </TableRow> <ListView android:id="@+id/tasklist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > </ListView> </RelativeLayout> ...

April 26, 2010 · 5 min · 1056 words · Micha Kops

Signing APK with the Maven-Jar-Signer Plugin

There is a nice Maven plugin helping you signing your Android app – the Maven Jar Signer Plugin. If you want to learn more about Maven integration in an android project take a look at this article. Maven Profile Setup Add the following code to your pom.xml <?xml version="1.0"?> <profiles> <profile> <id>sign</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jarsigner-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>signing</id> <goals> <goal>sign</goal> </goals> <phase>package</phase> <inherited>true</inherited> <configuration> <archiveDirectory/> <includes> <include>target/*.apk</include> </includes> <keystore>path/to/keystore</keystore> <storepass>storepasword</storepass> <keypass>keypassword</keypass> <alias>key-alias</alias> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <inherited>true</inherited> <configuration> <sign> <debug>false</debug> </sign> </configuration> </plugin> </plugins> </build> </profile> </profiles> ...

April 17, 2010 · 1 min · 155 words · Micha Kops

How to integrate Android Development Tools and Maven

With the Maven Android Plugin it is possible to build and deploy/undeploy your android app and start/stop the emulator – if you’re used to maven you won’t be going without it ;) If you’re interested in signing your apk using maven – take a look at this article Project Setup Create an android project using the android tool We need some dependencies – so create a pom.xml in the project’s root directory – I took this from the plugin samples and modified it: <?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (C) 2009 Jayway AB Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.android.app</groupId> <artifactId>demo</artifactId> <packaging>apk</packaging> <name>hasCode.com - Sample Android App using the Maven Android Plugin</name> <version>0.1</version> <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <!--<finalName>${artifactId}</finalName>--> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <configuration> <sdk> <path>${env.ANDROID_HOME}</path> <platform>3</platform> </sdk> <deleteConflictingFiles>true</deleteConflictingFiles> </configuration> <extensions>true</extensions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project> ...

April 2, 2010 · 3 min · 453 words · Micha Kops