Archive

Archive for the ‘android java’ Category

Get the orientation of the phone

August 25th, 2009 No comments

(Android phone – how-to/example)

If your application is dependent on the orientation of the phone, you may like to take a look at android.view.OrientationEventListener package.

To create the OrientationListener you will need the current activity Context.

private void CreateOrientationListener(Context context)
{
   m_OEListener = new OrientationEventListener (context, SensorManager.SENSOR_DELAY_UI)
   {
   public void onOrientationChanged (int orientation)
   {
   // in orientation you will find the orientation expressed in degrees

To enable/disable the listener:

public void OrientationEnable(boolean enable)
{
if (m_OEListener==null)
   return;

if (enable)
   m_OEListener.enable();
else
   m_OEListener.disable();
}

Note: You may choose to enable/disable while pausing/resuming the current activity.

Categories: android java Tags: ,

Broadcast Receiver

August 25th, 2009 No comments

(Android phone – how-to/example)

Android implements in an elegant manner the intent broadcast. One will use this, if for example you want to register to some actions (ex: one wants to know when something happens to the sdcard).

For the sdcard example, in order to register your “intentions”, use the following:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addDataScheme(”file”);
registerReceiver(m_brSDcardEvent, intentFilter);

where m_brSDcardEvent is the Broadcast Receiver itself, while the intentFilter is the filter where we register our interests.

To receive the intents and using them, is done in the Broadcast Receiver:

private final BroadcastReceiver m_brSDcardEvent = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();

if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
{
// SD card available
}
else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)
|| action.equals(Intent.ACTION_MEDIA_CHECKING))
{

….

all other intents will be treated similarly.

Note: do not forget to un-register your activity from the broadcast, if you do not want to alter the functionality of all other applications residing on the phone.

unregisterReceiver(m_brSDcardEvent);

The Message Handler

August 25th, 2009 No comments

(Android phone – how-to/example)

A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

This is how one will send an empty signaling message from a function to another.
In the main activity declare your handler:

private Handler m_myMsgHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
   switch(msg.what)
   {
   case R.id.message_id1:
   // do the according processing
….

In the caller method, one has just to send the empty message:

m_myMsgHandler.sendEmptyMessage(R.id.message_id1);

Alternatively, one can send a delayed message:

m_myMsgHandler.sendEmptyMessageDelayed(R.id.message_id1, longDelayMilliseconds);

Passing data from one caller method to the message handler is also straight-forward:

Message message = m_myMsgHandler.obtainMessage(R.id.message_id2, data);
message.sendToTarget();

where data, is the data you want to encapsulate (ex: byte[] data, int data etc)

In the message handler, in the switch, one will use the data as follows:


case R.id.message_id1:
   byte[] val = (byte[]) msg.obj;

Categories: android java Tags:

Using the vibrator, playing a DTMF sound

August 25th, 2009 No comments

(Android phone – how-to/examples)

So you want to make the Android vibe, with one of your actions. For this use android.os.Vibrator

Basically there are 2 lines of code:
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(milliseconds);

Playing a sound on Android is just as easy:
Use android.media.ToneGenerator

ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
tg.startTone(ToneGenerator.TONE_PROP_BEEP2);

Categories: android java Tags: , ,

How to get the disk free size?

August 25th, 2009 No comments

(Android phone – how-to/example)

This is a little different from Java, Android has a separate package that partly deals with this problem.
Use
android.os.Environment to get the sdcard (if needed).
android.os.StatFs – << Retrieve overall information about the space on a filesystem. This is a Wrapper for Unix statfs(). >>

The function is straight forward:

public static long GetFreeSpaceB()
{
   try
   {
     String storageDirectory = Environment.getExternalStorageDirectory().toString();
     StatFs stat = new StatFs(storageDirectory);
     return stat.getAvailableBlocks() * stat.getBlockSize();
   }
   catch (Exception ex)
   {
     return -1;
   }
}

Categories: android java Tags: