Thursday, April 15, 2010

Send sms between two simulator

First open the simulator batch file which is present in simulator folder of eclipse.
Then edit smssource 5000 and smsdestination 5001 and vice versa for next simulator then run both simulator we can send and receive sms between two simulator.

Message Listener in Blackberry

public class BackGroundApp extends Application implements OutboundMessageListener, javax.wireless.messaging.MessageListener
{
public static void main(String args[])
{
BackGroundApp theApp = new BackGroundApp();
theApp.enterEventDispatcher();
}

public BackGroundApp()
{
try
{
MessageConnection _mc = (MessageConnection)Connector.open("sms://:0");
_mc.setMessageListener(this);
}
catch (IOException e)
{
}

}

public void notifyIncomingMessage(MessageConnection messageconnection)
{
System.out.println("Coming here in outgoing SMS");
}

public void notifyOutgoingMessage(javax.wireless.messaging.Message message)
{
System.out.println("Coming here in outgoing SMS");
}
}

Friday, April 9, 2010

Popup Screen in Blackberry

final class SettingScreen extends PopupScreen
{
private final static int _Width = 200;
private final static int _Height = 200;
private final static int _X = (Display.getWidth() - _Width) >> 1;
private final static int _Y = (Display.getHeight() - _Height) >> 1;

public SettingScreen()
{
super( new VerticalFieldManager());
LabelField lf = new LabelField("Network Settings", LabelField.FIELD_HCENTER);
SeparatorField sf = new SeparatorField();
CheckboxField cbf1 = new CheckboxField(" WiFi", true);
CheckboxField cbf2 = new CheckboxField(" GPRS/Edge/3G", true);

VerticalFieldManager vfm = new VerticalFieldManager(VerticalFieldManager.FIELD_HCENTER);
ButtonField bf1 = new ButtonField("Save", ButtonField.FIELD_HCENTER);
ButtonField bf2 = new ButtonField("Cancel", ButtonField.FIELD_HCENTER);
vfm.add(bf1);
vfm.add(bf2);

//add UI components to Pop-up screen
add(lf);
add(sf);
add(cbf1);
add(cbf2);
add(vfm);
}
public int getPreferredWidth() {
return _Width;
}
public int getPreferredHeight() {
return _Height;
}

public void sublayout(int width, int height)
{
super.sublayout(getPreferredWidth(), Integer.MAX_VALUE);
setExtent(getPreferredWidth(), getPreferredHeight());
setPosition(_X, _Y);
}
}//end of class

Make call from the Application in Blackberry

Simple make ButtonField and add FieldChangeListiner to it.

call = new ButtonField("Click to Call",ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER);
call.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field , int context){
Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, new PhoneArguments(PhoneArguments.ARG_CALL,"1234567890"));
}
});

Wednesday, April 7, 2010

MapField in Blackberry Application

public class Map extends UiApplication
{
public static void main(String args[])
{
Map app = new Map();
app.enterEventDispatcher();
}
public Map()
{
pushScreen(new Scr ());
}
}

class CustomMapField extends MapField
{
Bitmap mIcon;
XYRect mDest;

public void moveTo(Coordinates coordinates) {
super.moveTo(coordinates);
mDest = null;
}

protected void paint(Graphics graphics) {
super.paint(graphics);
if (null != mIcon) {
if (null == mDest) {
XYPoint fieldOut = new XYPoint();
convertWorldToField(getCoordinates(), fieldOut);
int imgW = mIcon.getWidth();
int imgH = mIcon.getHeight();
mDest = new XYRect(fieldOut.x - imgW / 2, fieldOut.y - imgH, imgW, imgH);
}
graphics.drawBitmap(mDest, mIcon, 0, 0);
}
}
}

class Scr extends MainScreen implements FieldChangeListener {
CustomMapField mMapField;
Coordinates mCoordinates;
handleGPS hg;
VerticalFieldManager vfm;
ButtonField zoomin;
ButtonField zoomout;
ObjectChoiceField _moveUnits;
BasicEditField _moveByText;
ButtonField _moveTo;
public Scr() {
LocationProvider provider = null;
Location location = null;
double latitude = 51.507778;
double longitude = -0.128056;
//double latitude = 28.38;
// double longitude = 77.12;

zoomin = new ButtonField("-",ButtonField.CONSUME_CLICK);
zoomout = new ButtonField("+",ButtonField.CONSUME_CLICK);

vfm = new VerticalFieldManager()
{
protected void sublayout(int maxWidth,int maxHeight)
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();

super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
// double latitude1 = handleGPS.latitude;
// double longitude1 = handleGPS.longitude;
Coordinates mCoordinates = new Coordinates(latitude, longitude, 0);

try {
provider = LocationProvider.getInstance(null);
} catch (LocationException e) {
e.printStackTrace();
}
try {
location = provider.getLocation(-1);
} catch (LocationException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//mCoordinates = location.getQualifiedCoordinates();
vfm.add(new LabelField("Latitude: "+ String.valueOf(Coordinates.convert(mCoordinates.getLatitude(),
Coordinates.DD_MM_SS))));
vfm.add(new LabelField("Longitude: "+ String.valueOf(Coordinates.convert(mCoordinates.getLongitude(),
Coordinates.DD_MM_SS))));
vfm.add(zoomin);
zoomin.setChangeListener(this);
vfm.add(zoomout);
zoomout.setChangeListener(this);
vfm.add(_moveUnits = new ObjectChoiceField("Units: ", new String[] { "pixels", "degrees"}
));
vfm.add(_moveByText = new BasicEditField("Move horizontal, vertical: ","", 20, BasicEditField.NO_NEWLINE));
vfm.add(_moveTo = new ButtonField("Move"));
_moveTo.setChangeListener(this);

mMapField = new CustomMapField();
mMapField.mIcon = Bitmap.getBitmapResource("okred.PNG");
mMapField.moveTo(mCoordinates);
vfm.add(mMapField);
add(vfm);
}
public void fieldChanged(Field field, int context)
{
if (field == zoomin)
{
mMapField.setZoom(Math.max(mMapField.getZoom() - 1,
mMapField.getMinZoom()));
}
else if (field == zoomout)
{
mMapField.setZoom(Math.min(mMapField.getZoom() + 1,
mMapField.getMaxZoom()));
}
else if (field == _moveTo)
{
String amount = _moveByText.getText();
int ix = amount.indexOf(",");
int horizontalDelta = 0;
int verticalDelta = 0;

if (ix == -1)
{
ix = amount.indexOf(" ");
}

try
{
horizontalDelta = Integer.parseInt
(amount.substring(0, ix).trim());
verticalDelta = Integer.parseInt
(amount.substring(ix + 1, amount.length()).trim());
}
catch (NumberFormatException nfe)
{
Dialog.alert("Bad value!");
}

if (_moveUnits.getSelectedIndex() == 0)
{
mMapField.move(horizontalDelta, verticalDelta);
}
else
{
horizontalDelta += mMapField.getLongitude();
verticalDelta += mMapField.getLatitude();
mMapField.moveTo(verticalDelta, horizontalDelta);
}
}
}
}

Splash Screen in Blackberry

class SampleStart extends MainScreen
{


Bitmap b1;
BitmapField img1;
SampleDetail screen;
public SampleStart()
{
b1 = Bitmap.getBitmapResource("Images/splash1.PNG");
img1 = new BitmapField(b1,BitmapField.FIELD_HCENTER);
add(img1);
screen = new CarmapDetail();
startLoading();
}
public void startLoading() {
Thread loadThread = new Thread() {
public void run() {
// Make sure to invokeLater to avoid problems with the event thread.
try{
// Simulate loading time
Thread.sleep(1000);
} catch(java.lang.InterruptedException e){}



UiApplication.getUiApplication().invokeLater( new Runnable() {
public void run()
{
// This represents the next step after loading. This just shows
// a dialog, but you could push the applications main menu screen.
UiApplication.getUiApplication().pushScreen(screen);
//Dialog.alert("Load Complete");
}
});
}
};
loadThread.start();
}
}

Tuesday, March 30, 2010

Google Map in Blackberry App

GMLocation location = new GMLocation(52.527489, -0.082741, "Name","Address:");
invokeGMaps(location);

}
public void invokeGMaps(GMLocation l) {
int mh = CodeModuleManager.getModuleHandle("GoogleMaps");
if (mh == 0) {
try
{
throw new ApplicationManagerException("GoogleMaps isn't installed");
}
catch (ApplicationManagerException e)
{
System.out.println(e.getMessage());
}
}
URLEncodedPostData uepd = new URLEncodedPostData(null, false);
uepd.append("action", "LOCN");
uepd.append("a", "@latlon:" + l.getLatitude()
+ "," + l.getLongitude());
uepd.append("title", l.getName());
uepd.append("description", l.getDescription());
String[] args = { "http://gmm/x?" + uepd.toString() };
ApplicationDescriptor ad = CodeModuleManager
.getApplicationDescriptors(mh)[0];
ApplicationDescriptor ad2 = new ApplicationDescriptor(ad, args);
try
{
ApplicationManager.getApplicationManager() .runApplication(ad2, true);
}
catch (ApplicationManagerException e)
{
System.out.println(e.getMessage());
}
}


class FindMap extends MainScreen {
public FindMap() {
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(mInvokeGMaps);
}

}

}

MenuItem mInvokeGMaps = new MenuItem("Run GMaps", 0, 0) {
public void run() {
// GMLocation location = new GMLocation(52.507778, -0.128056, "London");
GMLocation location = new GMLocation(52.527489, -0.082741, "Name","Address:");
invokeGMaps(location);
};
};
public void invokeGMaps(GMLocation l) {
int mh = CodeModuleManager.getModuleHandle("GoogleMaps");
if (mh == 0) {
try
{
throw new ApplicationManagerException("GoogleMaps isn't installed");
}
catch (ApplicationManagerException e)
{
System.out.println(e.getMessage());
}
}
URLEncodedPostData uepd = new URLEncodedPostData(null, false);
uepd.append("action", "LOCN");
uepd.append("a", "@latlon:" + l.getLatitude()
+ "," + l.getLongitude());
uepd.append("title", l.getName());
uepd.append("description", l.getDescription());
String[] args = { "http://gmm/x?" + uepd.toString() };
ApplicationDescriptor ad = CodeModuleManager
.getApplicationDescriptors(mh)[0];
ApplicationDescriptor ad2 = new ApplicationDescriptor(ad, args);
try
{
ApplicationManager.getApplicationManager() .runApplication(ad2, true);
}
catch (ApplicationManagerException e)
{
System.out.println(e.getMessage());
}
}
}
class GMLocation {
String mName;
String mDescription;
double mLatitude;
double mLongitude;
public GMLocation(double lat, double lon) {
mLatitude = lat;
mLongitude = lon;
}
public GMLocation(double d, double e, String name) {
this(d, e);
mName = name;
}
public GMLocation(double lat, double lon, String name, String descr) {
this(lat, lon, name);
mDescription = descr;
}
public String getName() {
return mName;
}
public String getDescription() {
return mDescription;
}
public String getLongitude() {
return String.valueOf(mLongitude);
}
public String getLatitude() {
return String.valueOf(mLatitude);
}
}