Pages

Subscribe:

Sunday, February 27, 2011

Getting the IP address of Android Emulator in code.




Following code snippet explains the way to get the IP address of the Android Emulator.

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

Thursday, February 24, 2011

PHP on Android: Run SL4A + PHP in Android Emulator via Eclipse.


This tutorial assumes that you have set up Eclipse with Android SDK as described here http://developer.android.com/sdk/installing.html. Make sure to install Android SDK 1.6. You will need to create an Android Virtual Device (AVD) that runs on platform 1.6 – we will do this later.
There are really two stages to this project: get SL4A (Scripting Languages For Android) working in the emulator and then install PhpForAndroid.apk on your virtual device. Let’s get started.

1. Install TortoiseHG for Windows from here http://mercurial.selenic.com/downloads/ We need this to download the source of SL4A that is kept in a Mercurial repository. TortoiseHG is a program to manage Mercurial repositories.
2. Go to the directory where you want to download the source of SL4A, I suggest your Eclipse workspace. Right click and click on TortoiseHG -> Clone.. In the source enter the url that appears under Command Line access here http://code.google.com/p/android-scripting/source/checkout, currently https://android-scripting.googlecode.com/hg/android-scripting
3. Open Eclipse. Click File->Imports. Then Select an Import Source -> General -> Existing Projects into Workspace. Then Next. Select root directory. Browse to the folder where you downloaded the SL4A source (folder called android-scripting, by default). That should automatically import all the files.
4. Now, if you see that there are error indicators next to the folders (little red x’s) in the Package Explorer, then this is most likely a build path issue.
In Eclipse, go to Window->Preferences->Java->Build Path->Classpath Variables. Click New. Enter Name: ANDROID_SDK. Path: browse to the folder where the Android SDK is located.
For example, I downloaded and unpacked the SDK to E:\AndroidSDK\, so I automatically got this: E:\AndroidSDK\android-sdk-windows. So in the Path section I browsed to E:\AndroidSDK\android-sdk-windows. Click OK. That should set the correct build path and remove the errors.
If you still see error messages, then right click on the file (in the Package Explorer) and select Build Path -> Configure Build Path. Then inspect the path. Sometimes it might be set to a non-existent sdk version (happened to me with the BluetoothFacade).
5. Time to launch the emulator!! Now, I only tried 2 AVD’s: 2.1, which failed and then AVD 1.6 which worked. So I suggest you make sure you have 1.6 AVD ready to go. If not, we’ll make one in a second.
So, in Package Explorer select ScriptingLayerForAndroid (it might have a warning sign next to it but that won’t stop it from running). Click Run As.. -> Run Configurations. The Name will say ScriptingLayerForAndroid, click on the Target tab, then click on Manager … button and Android SDK and AVD manager appears.
Click New.. Enter a Name such as Android_1_6 and select a Target of Android API 1.6. Set the SD Card size to 9 or more.
Click Create AVD.
Make sure the new device appears and then close the window. Back in Run Configurations window, select Automatic and check mark Android_1_6. Click Run. Pray. The emulator should start and install the SL4A.
Once the emulator is open, open the SL4A app and play around. Use this as your guide http://code.google.com/p/android-scripting/wiki/UserGuide. Basically, the Menu button (right pane on the emulator) is your friend.
6. OK, so now I assume your SL4A app is installed and open. We are going to install the PHP package now.
Click on Menu (right side pane), then View->Interpreters. Right now it will just say Shell. Click on Menu->Add. Then click on PHP 5.3.3. The browser will open and try to download http://php-for-android.googlecode.com/files/phpforandroid_r1.apk Well, as of the writing of this tutorial, it’s not going to find the file. So, click on Menu->Go and fix the download path to http://php-for-android.googlecode.com/files/phpforandroid.apk Click Go. Once the package is downloaded click on Install.
Then, once you are back at (or open again) SL4A you will see a bunch of PHP scripts. Click on hello_world.php then select the round wheel icon. And there you go, you have PHP running on Android.

Increase Memory for Android Emulator


in Eclipse, go to "Debug Configurations". You can find that in the drop-down under the "debug" icon. Select "target", and select a preferred emulator target to launch. Then under "additional emulator command line options," add this:
-partition-size 1024

Thursday, February 17, 2011

Android - FlipView Sample Code

Screen



Layout

<?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"
>
<Button android:id="@+id/flip_me"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flip Me!"
/>
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FF00FF00"
android:text="This is the first panel"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFF0000"
android:text="This is the second panel"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFFFF00"
android:text="This is the third panel"
/>
</ViewFlipper>
</LinearLayout>

Java Code

package com.flipper.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class FlipperSample extends Activity {

ViewFlipper flipper;

/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setContentView(R.layout.main);

        flipper=(ViewFlipper)findViewById(R.id.details);

        Button btn=(Button)findViewById(R.id.flip_me);

        btn.setOnClickListener((View.OnClickListener) new flipMyView());
        
    }

class flipMyView implements View.OnClickListener {
public void onClick(View view) {
flipper.showNext();
}
}
}

Android - How to find Bluetooth Status

Below is a  sample code to check the bluetooth status in Android


package com.blue;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Toast;

public class BluetoothStatus extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

String toastText;
if (bluetooth.isEnabled()) {
String address = bluetooth.getAddress();
String name = bluetooth.getName();
toastText = name + " : " + address;
} else
toastText = "Bluetooth is not enabled";

Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();

bluetooth.setName("Enrichware");

}
}

Monday, February 14, 2011

Android - Text To Speach

Here is a sample program for Text to Speach

Screen



Layout


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget33"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_x="0px"
android:layout_y="0px"
>
</TextView>
<EditText
android:id="@+id/txtMessage"
android:layout_width="207px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="68px"
android:layout_y="39px"
>
</EditText>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_x="20px"
android:layout_y="48px"
>
</TextView>
<Button
android:id="@+id/btnSpeakNow"
android:layout_width="146px"
android:layout_height="wrap_content"
android:text="Speak Now!"
android:layout_x="96px"
android:layout_y="121px"
>
</Button>
</AbsoluteLayout>


Java Code

package com.text2speech;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Text2Speech extends Activity implements OnInitListener {
public TextToSpeech tts;

TextView txtMessage;
Button btnSpeakNow;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txtMessage = (TextView) findViewById(R.id.txtMessage);
btnSpeakNow = (Button) findViewById(R.id.btnSpeakNow);

btnSpeakNow.setOnClickListener((OnClickListener) new Speak());
btnSpeakNow.setEnabled(true);

Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, 0);
tts = new TextToSpeech(this, this);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
btnSpeakNow.setEnabled(true);
}

@Override
public void onInit(int status) {

}

class Speak implements Button.OnClickListener {

@Override
public void onClick(View arg0) {

String speech1 = txtMessage.getText().toString();
tts.setLanguage(Locale.US);
tts.speak(speech1, TextToSpeech.QUEUE_FLUSH, null);
String speech2 = "Neenga Yeppadi Irukkenga?";
String speech3 = "Enrichware Yeppadi Irkku?";
tts.speak(speech2, TextToSpeech.QUEUE_ADD, null);
tts.speak(speech3, TextToSpeech.QUEUE_ADD, null);
}
}

}