Archive for the ‘Android Dev’ Category

Screen ON And Unlock keypad

February 3, 2011 Leave a comment

Screen ON: Required when activity is in foreground and performing long running task
Use below code snippet And add "android.permission.WAKE_LOCK" in app manifest

PowerManager powermgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = powermgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
// call long running task

Unlock keypad: Required when using intents. example: Making a phone call with user activity and after completing phone call user acitivity shall not be held with a keylock

Use below code snippet inside Onresume() method of the activity. And add "android.permission.DISABLE_KEYGUARD" in app manifest
public void checkKeypadLock()
KeyguardManager Keylockmgr = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
Keylockmgr.newKeyguardLock("My Tag").disableKeyguard();

Complete Usage of Disabling and Reenabling keyguard on Android

if (mKeyguardManager == null) {
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock1 = mKeyguardManager.newKeyguardLock("QApp");
mKeyguardEnabled = true;

PowerManager powermgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = powermgr.newWakeLock(PowerManager.FULL_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE| PowerManager.ACQUIRE_CAUSES_WAKEUP,"QApp");


if (mKeyguardEnabled) {

if(mProductID == 1)
retval = mCalculator.Calc(mcalcJob,isOnline);
else if(mProductID == 2)
retval = m_pCalculator.Calc(mcalcJob,isOnline);


// ON sony ericsson the keylock is very quick, so we wait for 3Sec before reenabling keyguard only on Xperia 10i
try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block

Categories: Android Dev

Android Application Screen Orientation issues

January 20, 2011 1 comment

Screen Orientation issues to know when designing Android Application

1. To design application in Portrait Mode only add screen orientation attribute in manifest.xml, make sure you add inside activity,


2. Sometimes views/Controls in the application initializes on change in Orientation. add android:configChanges in manifest.xml, inside activity attribute


wish to manipulate with Keyboard use

android:configChanges="keyboardHidden|Orientation" this is more useful when application is installed on hard keyboard devices.

Categories: Android Dev

Android NDK Error Reference for noob

October 7, 2010 3 comments

Android NDK Error Reference

Error 1:
$ ndk-build
/cygdrive/c/andy/abc/obj/local/armeabi-v7a/objs/abc//hellow.o.d:1: *** multiple target patterns. Stop.

quick fix: Delete Obj folder from “C:\andy\prjM\obj\local\armeabi-v7a” then run ndk-build
or refer this

Error 2: *** commands commence before first target. Stop.
fix: Check there are no comments,no space ,no empty line in the src includes of

For example:
LOCAL_SRC_FILES :=file1.cpp\


3rd line has #, 4th line has space(check with cursor),5th line is empty line

LOCAL_SRC_FILES :=file1.cpp\

Error 3:
$ ndk-build clean
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/cygdrive/c/android-ndk-r4b/build/core/ *** Android NDK: Aborting . Stop.

Fix: include location inside global and
Run the command from the parent directory with and resides

Error 4:

Please define ANDROID_NDK_ROOT to point to the root of your
Android NDK installation.

Use case while executing shell script

xxx-desktop:~/bin/u/android-ndk-r5/build/tools$ ./
Please define ANDROID_NDK_ROOT to point to the root of your
Android NDK installation.

// Run the script inside NDK root directory like shown below
xxx-desktop:~/bin/u/android-ndk-r5/build/tools$ cd ..
xxx-desktop:~/bin/u/android-ndk-r5/build$ cd ..
xxxx-desktop:~/bin/u/android-ndk-r5$ ./build/tools/

Error 5: NDK r5 app(native-activity,native-audio,native-plasma) build problem

Compiling native-activity,native-audio,native-plasma on ndk-r5 give compile errors stating

header not found and  so on ……

Quick fix:  Rebuild all prebuilt

i;e execute shell script to build on latest toolchain provided by android which will take for a while (atleast on my pc)

xxx-desktop:~/bin/u/android-ndk-r5$ ./build/tools/
To follow build in another terminal, please use: tail -F /tmp/ndk-toolchain/build-CtAG7s/log.txt
Download sources from
Using git clone prefix: git://
downloading sources for toolchain/binutils
downloading sources for toolchain/build
downloading sources for toolchain/gcc
downloading sources for toolchain/gdb
downloading sources for toolchain/gmp
downloading sources for toolchain/gold
downloading sources for toolchain/mpfr
Patching toolchain sources
Toolchain sources downloaded and copied to /tmp/ndk-toolchain/build-CtAG7s/src
Cleaning up...
Building arm-eabi-4.4.0 toolchain... (this can be long)
ERROR: Could bot build arm-eabi-4.4.0 toolchain!

Now change to native-activity folder and call ndk-build for successful

xxx-desktop:~/bin/u/android-ndk-r5/samples/native-activity$ ndk-build
Compile thumb  : native-activity <= main.c
Compile thumb  : android_native_app_glue <= android_native_app_glue.c
StaticLibrary  : libandroid_native_app_glue.a
SharedLibrary  :
Install        : => libs/armeabi/

Categories: Android Dev

*** multiple target patterns issue in Cygwin

October 7, 2010 1 comment

follow the discussion in the 2nd reply from the board message here;

*EDIT* seem the updated eclipse forum moved the board message, a quick fix is try to update make with latest version shall fix *** multiple target patterns issuee

Categories: Android Dev

how to use Logs in Android Application and Android NDK

October 4, 2010 Leave a comment

We will first see how to use logs on Android Application side and then we look into usage of logs on Native side(NDK)

On Application side
Import log provided by Android

import android.util.Log;

then use below two lines of code where ever needed

String TAG =”ModuleName”;
Log.d(TAG,”File Name is =%s”+ testFilename );

Use following based on the priority constants

Priority Usage

INFO Log.i
WARN Log.w

WTF(What a Terrible Failure): Report an exception that should never happen.

Inside Native code
Similarly in Native code include the header and call the ‘ __android_log_print’ , ‘__android_log_write’ with appropriate priority constants, do not miss to the library in the fail to which leads to linker issues.

Add below line in


Include header

#include “android/log.h”
then call

__android_log_print(ANDROID_LOG_INFO, “ModuleName FunctionName”, “Enter”);

__android_log_print(ANDROID_LOG_INFO, “ModuleName FunctionName”, “Exit”);


__android_log_write(ANDROID_LOG_ERROR, “ModuleName FunctionName”, “#1”);

use below priority constants or find directly under ‘C:\android-ndk-r4b\build\platforms\android-8\arch-arm\usr\include\android’

ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */

Categories: Android Dev, Android-NDK

How to Analyse Stack trace on Android

September 27, 2010 2 comments

This article involves on how to Analyze stack trace on ANDROID, although there are tools available on how to make analysis on stack trace but I personally followed the below procedure.

1. Take a object dump of the the library(.so) using provided android tools

C:\android-ndk-r4b\build\prebuilt\windows\arm-eabi-4.2.1\bin>arm-eabi-objdump -S C:\andy\workspace\aolqaOEM\obj\local\armeabi-v7a\libaolqaWrapperDll.s
o > aolqalib.asm


C:\android-ndk-r4b\build\prebuilt\windows\arm-eabi-4.2.1\bin>arm-eabi-objdump -dR C:\andy\workspace\aolqaOEM\obj\local\armeabi-v7a\libaolqaWrapperDll.s
o > aolqalib.asm

aolqalib.asm is now disassembled, which mean you are now able to trace address assigned to each function in the library.

2. Copy aside the stack trace from the log-cat view of carbide.

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: ‘google/passion/passion/mahimahi:2.2/FRF91/43546:user/release-keys’
pid: 864, tid: 872 >>> xyz <<<
signal 11 (SIGSEGV), fault addr 002b3000
r0 47c23f10 r1 002b2fe8 r2 000b9e01 r3 00000008
r4 47ba9008 r5 00134d49 r6 002380c0 r7 80a4b724
r8 00000001 r9 474ea970 10 00291bf8 fp 0013f200
ip 00000000 sp 474ea320 lr afd1d56d pc afd0f1bc cpsr 20000010
d0 643a64696f72646e d1 6472656767756265
d2 0000002800000000 d3 0000000000000000
d4 0000000000000000 d5 0000000000000000
d6 0000000000000000 d7 0000000000000000
d8 46fdf80042e00000 d9 0000000000000000
d10 0000000000000000 d11 0000000000000000
d12 0000000000000000 d13 0000000000000000
d14 0000000000000000 d15 0000000000000000
d16 8000000000000000 d17 7e37e43c8800759c
d18 4085600000000000 d19 bfad7afccc419668
d20 4000000000000000 d21 3f114affd5703473
d22 bebbaaebd181c43f d23 3fd48e703a724000
d24 3e66376972bea4d0 d25 bff0000000000000
d26 3ff1687200000000 d27 bfad7afccc419669
d28 bffb22b279caa806 d29 3fd48e70431efbb1
d30 3c591e88027bf155 d31 3e41597762000000
scr 20000012
#00 pc 0000f1bc /system/lib/
#01 pc 0001d56a /system/lib/
#02 pc 0000fccc /data/data/xyz/lib/
code around pc:
afd0f19c f5d1f000 f5d1f040 e2522040 3a000009
afd0f1ac f5d1f080 f5d1f0c0 f5d1f100 f421020d
afd0f1bc f421420d f5d1f100 e2522040 f400022d
afd0f1cc f400422d 2afffff8 e2822040 e2522020
afd0f1dc 3a000003 f421020d e2522020 f400022d
code around lr:
afd1d54c bf00e7ee 4606b570 ef5cf7f1 46281c45
afd1d55c fbdcf7ef b1184604 462a4631 edeaf7f1
afd1d56c bd704620 bf004b06 b510a200 189b4c05
afd1d57c 7280f44f 4621191c f840f000 bd104620
afd1d58c 00024db0 000022ac b5f02800 b085460c
474ea2e0 00000001
474ea2e4 474ea970
474ea2e8 00291bf8 [heap]
474ea2ec afd0c6c7 /system/lib/
474ea2f0 80a45141 /data/data/xyz/lib/
474ea2f4 474ea844
474ea2f8 00000011
474ea2fc 00000032
474ea300 80a4b724 /data/data/xyz/lib/
474ea304 afd0cd31 /system/lib/
474ea308 474ea844
474ea30c 80a4b7a8 /data/data/xyz/lib/
474ea310 00134d49 [heap]
474ea314 002380c0 [heap]
474ea318 df002777
474ea31c e3a070ad
#00 474ea320 47ba9008
474ea324 afd1d56d /system/lib/
#01 474ea328 80bb8250 /data/data/xyz/lib/
474ea32c 80a405d9 /data/data/xyz/lib/
474ea330 002380c0 [heap]
474ea334 80a0fcd1 /data/data/xyz/lib/

3. Now look at the last function call from user defined library in ourcase its, the program counter (pc) value is 0000fccc, which triggers the next function call in @pc value 0001d56a and which in turn call another function @pc value 0000f1bc in

Open the disassembled file and try to find the first PC value i,e 0000fccc exclude zeros before find only ‘fccc’ without inverted commas will fetch you something like this

fcc2: 980f ldr r0, [sp, #60]
fcc4: f8cd 3510 str.w r3, [sp, #1296]
fcc8: f8cd 3514 str.w r3, [sp, #1300]
fccc: f7fd fc64 bl d598
fcd0: f8dd 353c ldr.w r3, [sp, #1340]
fcd4: f50d 6ca6 add.w ip, sp, #1328 ; 0x530

if you scroll upwards you will find function call starting from 0000f8d0 which is the address assigned to function in library,so the function 0000fccc is called inside this method.

0000f8d0 :
f8d0: e92d 4ff0 stmdb sp!, {r4, r5, r6, r7, r8, r9, sl, fp, lr}
f8d4: ed2d 8b02 vstmdb sp!, {d8}
f8d8: f5ad 6da8 sub.w sp, sp, #1344 ; 0x540
f8dc: b081 sub sp, #4
f8de: 4cc6 ldr r4, [pc, #792] (fbf8 )
f8e0: af34 add r7, sp, #208


now call on function indicates clearly that the crash was somewhere inside SetToCString method of CNewStdString.

Hope this helps, next session will try to write an article on how to hide function address inside .so file

Categories: Android Dev

Using AsyncTasks in Android

September 22, 2010 3 comments

AsyncTasks, why do we need it?

As we are aware that any User interaction on any Android application should and must response with in 5 seconds, failure to which results in ANR Dialog. Yes “Application Not Responding” Dialog with ‘Wait’ and ‘Force Close’ inputs. Asynchronous operations can also be performed using Threads in accordance with Handler and messages to update UI about the action progress.

Here we will discuss code snippets on how AsyncTask can be incorporated to perform Synchronous operations updated the progress of operation.

Three types are used by AsyncTask, in this case datatype is a String,

First param String, this type of the parameters sent to the task upon execution.
Second param , this type of the Strings contains progress units published during the background operation.
Third param Result, this strings contains the result of the background operation.

Note* when no params are required use type void.

A class extend AsyncTask which triggers/perform long running tasks along side in override methods of AsyncTasks.
such as in our case
1. doInBackground(…)
2. onPreExecute()
3. rogressUpdate(…)
4. onCancelled()
5. onPostExecute(…)
Note* ‘…’ denotes function parameters

when myClass issues call to execute(…)

myClass.execute(“String”); //where “String” is the first param of the AsyncTask function call.

myClass.cancel(true);// onCancelled() is called.

below we will see the intention of each function call

protected class myClass extends AsyncTask

// Note that first param in the class definition matches the param passed to this method
// and the last param in the class definition matches the return type of this method
protected String doInBackground( String... params )
//-- on every iteration
//-- runs a while loop that causes the thread to sleep for 50 milliseconds
//-- publishes the progress - calls the onProgressUpdate handler defined below
//-- and increments the counter variable i by one
for(int i=0; i<=n; i++)
// longrunningtask();
publishProgress( i );


//when user issues a cancel on the operation.
protected void onCancelled() {
// Do a cleanup on operation canceled
// ReInitialise Data

// on successful completion of onPostExecute method is called
protected void onPostExecute(String result) {



// Called before doInBackground()
protected void onPreExecute() {
// do any initialization

// update the UI on complete of longrunningtask()
protected void onProgressUpdate(String... values) {


Next article will follow on Services in Android.

Categories: Android Dev