Archive

Posts Tagged ‘useful activities’

How to create a new Android activity

August 25th, 2009 2 comments

(Android phone – how-to/example)

The Android development paradigm introduces for the developer the “Activity”: <> During development you will find it easy to wrap-up parts of the functionality of your code into independent activities, with their own life cycle. More about activity life cycle on the Android developers website.

Easy (just start activity)
Assume you have the MainActivity, and a SecondaryActivity you want to start, this may be done in the easiest way:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(this, SecondaryActivity.class.getName());
startActivity(intent);

Medium (pass some params as well)
Now imagine you want to pass some data to the new intent. You will achieve this using the Bundle class, to encapsulate your data. Example:

Intent intent = new Intent();
Bundle bun = new Bundle();

bun.putString(”param_string”, “the actual string”); // add two parameters: a string and a boolean
bun.putBoolean(”param_bool”, true);

intent.setClass(this, SecondaryActivity.class);
intent.putExtras(bun);
startActivity(intent);

In the SecondaryActivity, you will need to access these params. This is how:

Bundle bun = getIntent().getExtras();
String param1 = bun.getString(”param_string”);
boolean param2 = bun.getBoolean(”param_bool”);

Advanced (return some data)
So we passed some parameters to the SecondaryActivity, but let’s assume we want some results as well. From MainActivity one will pack parameters and all others like in the examples above, and than one will call SecondaryActivity as follows:

startActivityForResult(intent, R.id.secondaryactivity_finished);

where secondaryactivity_finished is just some id, that the MainActivity will be looking for in it’s onActivityResult, as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     switch(requestCode)
     {
         case R.id.secondaryactivity_finished:
         if (resultCode == RESULT_OK)
         {
         Bundle res = data.getExtras();
         String result = res.getString(”param_result”);
         }
         break;
     } // end switch
}

In onActivityResult, one simply follows the activities that returned and their error/success codes, and as in the example above may get data passed by the SecondaryActivity. In our case SecondaryActivity returned a string under the name “param_result”.

On the other side, in SecondaryActivity, one will need to pack “param_result” as follows.

private void ReturnToParent()
{
   Bundle conData = new Bundle();
   conData.putString(”param_return”, “text returned to MainActivity”);
   Intent intent = new Intent();
   intent.putExtras(conData);
   setResult(RESULT_OK, intent);
   finish();
}

Some useful default activities:
Part of the beauty of Android is that you can encapsulate other functionality/activities in your own code, just by writing a few lines of code. Below is a list of useful activities one may consider to use:

1. Writing an email

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(”mailto:office@example.com”));
intent.putExtra(”subject”, “my subject”);
intent.putExtra(”body”, “my message”);
startActivity(intent);

2. Browse to a web-page

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(”http://www.google.com”));
startActivity(intent);

3. Write a SMS

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(”sms://”));
intent.putExtra(”address”, “”);
intent.putExtra(”sms_body”, “my message”);
startActivity(intent);

4. Search something on Google

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, “search this text”);
startActivity(intent);

5. Get the Wiktionary of some word

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(”http://en.wiktionary.org/wiki/” + “word”));
startActivity(intent);

6. Get the Wikipedia page of some words

String uri = “http://en.wikipedia.org/wiki/” + “my text”;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

There are several others activities one can try, just a quick google-search will reveal useful examples and links:
- calendar, phone, map, getdirections, product/book search – here

Mezzofanti – Install&Run Tutorial

August 20th, 2009 4 comments

(Android phone – application, how-to)

This tutorial shows you how to run the Mezzofanti application from your Android SDK:
1. Download and install Android SDK
2. Download and install Android NDK
(optional) read our post on how to interface C/C++ with Java, with example on Tesseract, here.
3. Download Mezzofanti source code and patches

Note: from now on we will consider the ndk installed in [ndk_path]

4. Create directory “tesseract” in [ndk_path]/sources
5. Copy Tesseract 2.03 in [ndk_path]/sources/tesseract. Copy them directly, for example “Makefile” should be seen directly in [ndk_path]/sources/tesseract.

6. Unzip all downloaded files (step 3), in the directory [ndk_path]/sources/tesseract
7. In [ndk_path]/apps make a symbolic link to “mezzofanti_java_code”
After this step, the directory /sources/tesseract should look like:

-rw-r–r– 1 User None 170 Jul 8 14:38 AUTHORS
-rwxr-xr-x 1 User None 12916 Jul 8 14:38 Android-multiple-libs.mk
-rwxr-xr-x 1 User None 5997 Jul 9 17:40 Android.mk
-rwxr-xr-x 1 User None 71 Jul 9 15:35 Application.mk
-rw-r–r– 1 User None 17751 Jul 8 14:38 BUILD
-rw-r–r– 1 User None 1058 Jul 8 14:38 COPYING
-rw-r–r– 1 User None 3014 Jul 8 14:38 ChangeLog
-rw-r–r– 1 User None 9236 Jul 8 14:38 INSTALL
-rw-r–r– 1 User None 1223 Jul 8 14:38 LICENSE
-rw-r–r– 1 User None 933 Jul 8 14:38 Makefile.am
-rw-r–r– 1 User None 45 Jul 8 14:38 NEWS
-rw-r–r– 1 User None 33 Jul 8 14:38 OWNERS
-rw-r–r– 1 User None 5014 Jul 8 14:38 README
-rw-r–r– 1 User None 8913 Jul 8 14:38 ReleaseNotes
-rw-r–r– 1 User None 286 Jul 8 14:38 StdAfx.cpp
-rw-r–r– 1 User None 778 Jul 8 14:38 StdAfx.h
-rw-r–r– 1 User None 7309 Jul 8 14:38 acinclude.m4
-rw-r–r– 1 User None 33298 Jul 8 14:38 aclocal.m4
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 aspirin
drwxr-xr-x+ 2 User None 0 Aug 31 18:35 ccmain
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 ccstruct
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 ccutil
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 classify
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 config
-rwxr-xr-x 1 User None 10209 Jul 8 14:38 configure.ac
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 cutil
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 dict
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 dlltest
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 doc
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 helium
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 image
drwx——+ 3 User None 0 Aug 28 10:28 java
drwx——+ 9 User None 0 Sep 1 17:22 mezzofanti_java_code
-rwx——+ 1 User None 42136 May 29 21:54 liblog.so
-rwxr-xr-x 1 User None 437 Jul 8 14:38 makemoredists
drwxr-xr-x+ 3 User None 0 Jul 8 14:38 neural_networks
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 pageseg
-rwxr-xr-x 1 User None 2140 Jul 8 14:38 runautoconf
drwxr-xr-x+ 6 User None 0 Aug 31 18:39 tessdata
-rw-r–r– 1 User None 9200 Jul 8 14:38 tessdll.cpp
-rw-r–r– 1 User None 38429 Jul 8 14:38 tessdll.dsp
-rw-r–r– 1 User None 5421 Jul 8 14:38 tessdll.h
-rwxr-xr-x 1 User None 159480 Jul 8 14:38 tessdll.vcproj
-rwxr-xr-x 1 User None 38795 Jul 8 14:38 tesseract.dsp
-rwxr-xr-x 1 User None 2117 Jul 8 14:38 tesseract.dsw
-rwxr-xr-x 1 User None 4709 Jul 8 14:38 tesseract.sln
-rw-r–r– 1 User None 5905 Jul 8 14:38 tesseract.spec
-rwxr-xr-x 1 User None 125592 Jul 8 14:38 tesseract.vcproj
-rw-r–r– 1 User None 560 Sep 1 17:36 test
drwxr-xr-x+ 3 User None 0 Aug 28 10:28 testing
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 textord
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 training
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 viewer
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 wordrec

8. Patch the Tesseract files, with this Tesseract_ccmain_patch.zip (found on the google code). Also do copy over the Android.mk file, over the original one in order to be able to compile the code.

9. In Eclipse, import the Mezzofanti project.

enjoy ;)