Monday, October 12, 2009

Location Manager Examples

Here theres is a small program regarding location manager and location class

package com.vinnysoft.ami;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

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

LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);

Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);

// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
}

private final LocationListener locationListener = new LocationListener()
{

@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
myLocation = (TextView) findViewById(R.id.myLocation);

String addressString = "no address found";

if(location!=null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;

double lattitude = location.getLatitude();
double longitude = location.getLongitude();

Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List
addresses= gc.getFromLocation(lattitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size()>0) {
Address address = addresses.get(0);
for(int i =0;i sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
}catch (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
myLocation.setText("your Current Position is :\n" +latLong + "\n " + addressString );
}
}


add mapview in main.xml file

give the permission sin android manifest file







Wednesday, September 2, 2009

Creating databases from SQLite shell

A simple file

Code Listing 1. Creating a contacts database

The Created DB file

Code Listing 2. The SQLite db file

How does it related to other DBMSs?

Code Listing 3. The API used for creating an SQLite db

Creating database from the Android Shell

Attach to an Android device where you want to create the database

Code Listing 4. Attaching to an Android device

Move to a data directory

Code Listing 5. A directory for creating the databases

Go to the SQLite shell and create db

Create a table

The Created file

Reopen the database


A simple file

In SQLite, the database is stored in one file. Creating a database is as simple as passing the name of the file to the SQLite command line program (for example: sqlite3.exe).

Code Listing 1. Creating a contacts database

D:\Research\sqlite\sqlite-3_5_7>sqlite3 contactsext.db
SQLite version 3.5.7
Enter ".help" for instructions

sqlite> CREATE TABLE contactsext (_id INTEGER, name TEXT);
sqlite> .tables
contactsext

sqlite> .exit

In the above code listing, sqlite3 is the command line program that can be downloaded from the SQLite site. I have installed (or ‘copied’/’unzipped’ the sqlite3.exe file into D:\Research\sqlite\... directory. When starting the program, contacts is provided as the parameter for the name of the database I would like to create.

The Created DB file

After the above command, if we look in the filesystem, we will see a contactsext.db file.

Code Listing 2. The SQLite db file

Directory of D:\Research\sqlite\sqlite-3_5_7

03/24/2008 11:19 AM 2,048 contactsext.db
03/17/2008 02:48 PM 440,651 sqlite3.exe

As you can see above, the contactsext.db file is created in the directory.

How does it related to other DBMSs?

The above database creation is vastly simplified, when you compare it with the full-fledged DBMSs like SQLServer, Oracle, etc. For example, there we have a ‘CREATE DATABASE’ command, which lets us specify one or more database files. This command will also let us specify initial and maximum sizes for these files, as well as specifying the file growth. In addition to the database files (which hold the actual data), we can also specify the log files, which log various database related events and changes.

SQLite simplifies the database creation and none of the above options are available. Of course, the above method is for creating an SQLite database from the command prompt. You can also create a new db programmatically by using the function sqlite3_open() (or, other related functions) from the SQLite API.

Code Listing 3. The API used for creating an SQLite db

SQLITE_API int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
SQLITE_API int sqlite3_open16(
const void *filename, /* Database filename (UTF-16) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
SQLITE_API int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
);

Creating database from the Android Shell

The above example uses creating SQLite database from a Windows Shell (‘DOS’ shell). The example below uses the Android shell to create an SQLite database.

Attach to an Android device where you want to create the database

Code Listing 4. Attaching to an Android device

C:\Users\Administrator>adb devices
List of devices attached
1 emulator-tcp-5555 device 0


C:\Users\Administrator>adb -d 1 shell
#

As shown above, first find out what Android devices are out there by using the ‘adb devices’ command. Then Attach to a particular device and start the shell on it (adb –d 1 shell).

Move to a data directory

Code Listing 5. A directory for creating the databases

# pwd
pwd
/data/data/com.infinitezest.contactsext/databases

As an example, I have a contactsext application, which stores its data in the /data/data//databases directory. Now I am still in the Android/Linux/OS shell.

Go to the SQLite shell and create db

Just like before, opening the sqlite3 command line and creating a contactsext database takes one command. The version here (in Android) is slightly behind (3.5.7 vs. 3.5.0). That’s probably going to continue as it might take a bit of time before the latest SQLite is incorporated into Android.

# sqlite3 contactsext.db
sqlite3 contactsext.db
SQLite version 3.5.0
Enter ".help" for instructions

Create a table

Then create a table and verify it has been created.

sqlite> CREATE TABLE contactsext (_id INTEGER, name TEXT);
CREATE TABLE contactsext (_id INTEGER, name TEXT);

sqlite> .tables
.tables
contactsext

sqlite> select * from sqlite_master;
select * from sqlite_master;
table|contactsext|contactsext|2|CREATE TABLE contactsext (_id INTEGER, name TEXT
)

The Created file

Then get out of the SQLite shell to get back into the OS shell, and see that the contactsext.db file has been created.

sqlite> .exit
.exit
# ls -l
ls -l
-rw-r--r-- root root 2048 2008-03-24 18:13 contactsext.db

Reopen the database

If you open the contactsext.db again, you will see the previous table in there.

# sqlite3 contactsext.db
sqlite3 contactsext.db
SQLite version 3.5.0
Enter ".help" for instructions

sqlite> .tables
.tables
contactsext



Creating databases from SQLite shell

A simple file

Code Listing 1. Creating a contacts database

The Created DB file

Code Listing 2. The SQLite db file

How does it related to other DBMSs?

Code Listing 3. The API used for creating an SQLite db

Creating database from the Android Shell

Attach to an Android device where you want to create the database

Code Listing 4. Attaching to an Android device

Move to a data directory

Code Listing 5. A directory for creating the databases

Go to the SQLite shell and create db

Create a table

The Created file

Reopen the database


A simple file

In SQLite, the database is stored in one file. Creating a database is as simple as passing the name of the file to the SQLite command line program (for example: sqlite3.exe).

Code Listing 1. Creating a contacts database

D:\Research\sqlite\sqlite-3_5_7>sqlite3 contactsext.db
SQLite version 3.5.7
Enter ".help" for instructions

sqlite> CREATE TABLE contactsext (_id INTEGER, name TEXT);
sqlite> .tables
contactsext

sqlite> .exit

In the above code listing, sqlite3 is the command line program that can be downloaded from the SQLite site. I have installed (or ‘copied’/’unzipped’ the sqlite3.exe file into D:\Research\sqlite\... directory. When starting the program, contacts is provided as the parameter for the name of the database I would like to create.

The Created DB file

After the above command, if we look in the filesystem, we will see a contactsext.db file.

Code Listing 2. The SQLite db file

Directory of D:\Research\sqlite\sqlite-3_5_7

03/24/2008 11:19 AM 2,048 contactsext.db
03/17/2008 02:48 PM 440,651 sqlite3.exe

As you can see above, the contactsext.db file is created in the directory.

How does it related to other DBMSs?

The above database creation is vastly simplified, when you compare it with the full-fledged DBMSs like SQLServer, Oracle, etc. For example, there we have a ‘CREATE DATABASE’ command, which lets us specify one or more database files. This command will also let us specify initial and maximum sizes for these files, as well as specifying the file growth. In addition to the database files (which hold the actual data), we can also specify the log files, which log various database related events and changes.

SQLite simplifies the database creation and none of the above options are available. Of course, the above method is for creating an SQLite database from the command prompt. You can also create a new db programmatically by using the function sqlite3_open() (or, other related functions) from the SQLite API.

Code Listing 3. The API used for creating an SQLite db

SQLITE_API int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
SQLITE_API int sqlite3_open16(
const void *filename, /* Database filename (UTF-16) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
SQLITE_API int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
);

Creating database from the Android Shell

The above example uses creating SQLite database from a Windows Shell (‘DOS’ shell). The example below uses the Android shell to create an SQLite database.

Attach to an Android device where you want to create the database

Code Listing 4. Attaching to an Android device

C:\Users\Administrator>adb devices
List of devices attached
1 emulator-tcp-5555 device 0


C:\Users\Administrator>adb -d 1 shell
#

As shown above, first find out what Android devices are out there by using the ‘adb devices’ command. Then Attach to a particular device and start the shell on it (adb –d 1 shell).

Move to a data directory

Code Listing 5. A directory for creating the databases

# pwd
pwd
/data/data/com.infinitezest.contactsext/databases

As an example, I have a contactsext application, which stores its data in the /data/data//databases directory. Now I am still in the Android/Linux/OS shell.

Go to the SQLite shell and create db

Just like before, opening the sqlite3 command line and creating a contactsext database takes one command. The version here (in Android) is slightly behind (3.5.7 vs. 3.5.0). That’s probably going to continue as it might take a bit of time before the latest SQLite is incorporated into Android.

# sqlite3 contactsext.db
sqlite3 contactsext.db
SQLite version 3.5.0
Enter ".help" for instructions

Create a table

Then create a table and verify it has been created.

sqlite> CREATE TABLE contactsext (_id INTEGER, name TEXT);
CREATE TABLE contactsext (_id INTEGER, name TEXT);

sqlite> .tables
.tables
contactsext

sqlite> select * from sqlite_master;
select * from sqlite_master;
table|contactsext|contactsext|2|CREATE TABLE contactsext (_id INTEGER, name TEXT
)

The Created file

Then get out of the SQLite shell to get back into the OS shell, and see that the contactsext.db file has been created.

sqlite> .exit
.exit
# ls -l
ls -l
-rw-r--r-- root root 2048 2008-03-24 18:13 contactsext.db

Reopen the database

If you open the contactsext.db again, you will see the previous table in there.

# sqlite3 contactsext.db
sqlite3 contactsext.db
SQLite version 3.5.0
Enter ".help" for instructions

sqlite> .tables
.tables
contactsext



Thursday, August 27, 2009

Zooming the ImageView

For Zooming the ImageView Here is the snippet :

public class Zoom extends View {
private Drawable image;
private int zoomControler=200;
public Zoom(Context context)
{
super(context);
image=context.getResources().getDrawable(R.drawable.gallery_photo_1);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//here u can control the width and height of the images........ this line is very important
image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
invalidate();
return true;
}
}


If any body knows the other way they can post here it should be helpful for others to develop.....

WebView in Android :

Web View In Android :

WebViewclassOverview click here to see the webview class details .

Here i am pasting the small example over webview implementation


package com.vinnysoft.webkit;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;

public class WebSample extends Activity {
/** Called when the activity is first created. */
private static final FrameLayout.LayoutParams ZOOM_PARAMS =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM);
private WebView wv;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main);
this.wv = (WebView) this.findViewById(R.id.webview);

FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.wv.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);

this.wv.loadUrl("http://imageurl.com/1.jpg");

// for example this.wv.loadUrl("http://news.softpedia.com/newsImage/RIM-Announces-BlackBerry-JDE-Plug-in-for-Eclipse-3.JPG");

like this example ....



}
}

In the res folder make the layout like this create a webview widget over there


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>


Wednesday, August 26, 2009

XML Layout in Java

Write Layouts in Java :

This is the file which help how to write eidgets in java file and instaniate there it self....

package com.vinnysoft.hello;

import com.vinnysoft.hello.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HelloWorld extends Activity {

// TextView that will be assigned to the
// TextView resource when it's inflated or
// created in code.
TextView myTextView;

// Variable used to determine if the layout
// should be inflated from XML or constructed
// in code.
private static boolean inflate = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

if (inflate)
inflateXMLLayout();
else
constructLayout();
}

// Use the 'main.xml' layout resource to create
// the UI for this Activity.
private void inflateXMLLayout() {
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.myTextView);
}

// Create the Activity's UI layout by creating
// and populating the layout in code.
private void constructLayout() {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
myTextView = new TextView(this);
myTextView.setText("Hello World, HelloWorld");
ll.addView(myTextView, textViewLP);
addContentView(ll, lp);
}
}


Androidmanifest file for the Programs ...


// application package
package="com.vinnysoft.hello">
// application icon path

//application activity permission with lable name

// intent filter what we need to launch






Proxy setting

To get the internet in emulator type the follow commands on sdk tools path .

// Proxy Settings

Open

Cd :\sdk\tools

adb shell



ls -l /data/data/com.android.providers.settings/databases/settings.db

sqlite3 /data/data/com.android.providers.settings/databases/settings.db

Open the databases

.databases

Show the tables


.tables

setting the proxy permission

insert into system values(99,'http_proxy','Server Address :Port Number');

insert into system values(99,'http_proxy','192.1.1.1:22');

or
insert into system values(101,'http_proxy','192.1.1.1:22');

Enjoy the Internet in Emulator

ADB Commands:

What is ADB?

Android Debugger Bridge....

Commands where i collected and used i am posting here for helpful to others...

// installing apk in system

adb install filename.apk

adb kill-server
adb start-server

adb devices // to get what are the devices running or attached

to uninstall the apk...

adb uninstall packagename.apk

To access the data bases and seeing the data in the content providers:

here one small example to open the content providers contacts

adb shell

# cd /data/data

# cd com.google.android.providers.contacts/databases

# ls

contacts.db

# sqlite3 contacts.db # it's picky you must include suffix
SQLite version 3.5.0

Enter ".help" for instructions

sqlite> .tables
_deleted_people android_metadata people
_sync_state calls peopleLookup
_sync_state_metadata contact_methods phones

sqlite> select * from people;


android:authorities="aexp.syncexample.SimpleString"
android:syncable="true"/>


for modification or inserting

sqlite>.dump

Friday, August 14, 2009

Sound Pool Manager Class

Here I am posting the Sound Pool Manager Class for playing the Sounds and Music from the resources.....

SoundPoolManager Class

package com.vinnysoft.music;
import android.media.SoundPool;
import android.media.JetPlayer;
class SoundPoolEvent
{
public SoundPoolEvent(int eventType,int eventSound)
{
this.eventType = eventType;
this.eventSound = eventSound;
}
public int eventType;
public int eventSound;

public static final int SOUND_PLAY=0;
public static final int SOUND_STOP=1;
public static final int SOUND_MUSIC_PLAY=2;
public static final int SOUND_MUSIC_PAUSE=3;
public static final int SOUND_MUSIC_STOP=4;
public static final int SOUND_MUSIC_RESUME=5;
}
class SoundStatus
{
public SoundStatus()
{

}
public static final int STATUS_LOOPING_NOT_STARTED=0;
public static final int STATUS_LOOPING_PAUSED=1;
public static final int STATUS_LOOPING_PLAYING=2;


}
public class SoundPoolManager implements Sound
{
SoundPoolManager(android.content.Context context)
{
this.context = context;
soundEvents = new java.util.LinkedList();
sounds = new java.util.HashMap();
handles = new java.util.HashMap();
streamIds = new java.util.HashMap();
isRunning = false;
finished = false;
this.musicPlayer =JetPlayer.getJetPlayer();
this.musicPlayer.loadJetFile(context.getResources().openRawResourceFd(R.raw.notify));
byte segmentId = 0;

this.musicPlayer.queueJetSegment(0, -1, -1, 0, 0, segmentId++);
}
public void addSound(int resid, boolean isLooping)
{

sounds.put(resid, new Boolean(isLooping));

}

public void startSound()
{
this.soundPool = new android.media.SoundPool(this.sounds.size(),android.media.AudioManager.STREAM_MUSIC,100);
java.util.Iterator iterator = sounds.keySet().iterator();

while(iterator.hasNext())
{
int soundid = iterator.next().intValue();
int soundhandle = this.soundPool.load(this.context, soundid, 1);
handles.put(new Integer(soundid), new Integer(soundhandle));
}


}
public void stopSound()
{
try
{
java.util.Iterator iterator = sounds.keySet().iterator();

while(iterator.hasNext())
{

int soundid = iterator.next().intValue();

this.soundPool.pause( this.handles.get(soundid).intValue());
this.soundPool.stop(this.handles.get(soundid).intValue());



}
}
catch(Exception e)
{

}
finally
{
try
{
this.musicPlayer.pause();
}
catch(Exception e1)
{

}
try
{
this.musicPlayer.release();
}
catch(Exception e2)
{

}
try
{
this.soundPool.release();
}
catch(Exception e3)
{

}

}


}

public int currentPlayer;
private boolean isRunning;
private java.util.HashMap sounds;
private java.util.HashMap handles;
private java.util.HashMap streamIds;
private android.content.Context context;
private java.util.LinkedList soundEvents;
private java.util.HashMap mediaPlayers;
public void stopSound(int resid)
{

}
public void playSound(int resid)
{
if(soundEvents!=null)
{
try
{
android.media.AudioManager mgr = (android.media.AudioManager) context.getSystemService(android.content.Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(android.media.AudioManager.STREAM_MUSIC);
int streamID = soundPool.play(handles.get( resid).intValue(), streamVolume, streamVolume, 1, 0, 1.0f);
int maxvolume = mgr.getStreamMaxVolume(android.media.AudioManager.STREAM_MUSIC);
mgr.setStreamVolume(android.media.AudioManager.STREAM_MUSIC, maxvolume, 0);
this.streamIds.put(resid, streamID);

}
catch(Exception e)
{

}
}
}
public void startMusic(int resid)
{

this.musicPlayer.play();

}
public void stopMusic(int resid)
{
this.musicPlayer.pause();
}
public void pauseMusic(int resid)
{
this.musicPlayer.pause();
}
public void resumeMusic(int resid)
{
this.musicPlayer.play();
}
SoundPool soundPool;
JetPlayer musicPlayer;

boolean finished = false;

}


And add the interface of Sound..... to the above package


package com.vinnysoft.music;

public interface Sound {

public void addSound(int resid, boolean isLooping);
public void startSound();
public void stopSound();
public void stopSound(int resid);
public void playSound(int resid);
public void startMusic(int resid);
public void stopMusic(int resid);
public void pauseMusic(int resid);
public void resumeMusic(int resid);

}

And u can use in difffernt ways ....

1. creating the soundpoolmanager instance class ......
2. by using Sound interface instance,....

SoundPoolManager m = new SoundPoolManager(context);
m.addSound(R.raw.vinny, false);
m.addSound(R.raw.soft,true);
m.startSound();
m.playSound(R.raw.vinny);
m.playMusic(R.raw.soft);



or

private Sound soundManager;

soundManager.playSound(R.raw.vinny);

Log.i("**********","kshasjklgfjkas");

public synchronized void stopMusic()
{
soundManager.stopSound();

//message.sendToTarget();
this.soundManager.stopSound();
}


By the above class you can play the music and sounds calling the above snippet....

for more details go through this example:

http://code.google.com/p/monolithandroid