Friday, July 31, 2009

Close Application BlackBerry By injecting Escape Key

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;

private void  closeCamera()
 {
EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN,Characters.ESCAPE, 50);
inject.post();
inject.post();
}

get the System Date Time Blackberry

Date d = new Date(System.currentTimeMillis());
lblDate = new LabelField(d.toString());

this will return Long Date Including timizone and all the things. but if you want a format the date time to shor, long or medium you can use following method.

Medium Date

SimpleDateFormat df = new SimpleDateFormat(SimpleDateFormat.DATE_MEDIUM); 
 String Mydate = df.format(new Date(System.currentTimeMillis()));
Ex:-Mar 08, 2006.

Short Date

SimpleDateFormat df = new SimpleDateFormat(SimpleDateFormat.DATE_SHORT); 
String Mydate = df.format(new Date(System.currentTimeMillis()));
Ex:-03.08.06.

Long Date

SimpleDateFormat df = new SimpleDateFormat(SimpleDateFormat.DATE_LONG); 
String Mydate = df.format(new Date(System.currentTimeMillis()));
Ex:-Wednesday, March 08, 2006.

Custom Date

SimpleDateFormat df = new SimpleDateFormat("MMMM, dd yyyy"); 
String Mydate = df.format(new Date(System.currentTimeMillis()));
Ex:-March 08, 2006.

Some Formating Examples

"yyyy.MM.dd G 'at' HH:mm:ss z" ------>  2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" ------------------> Wed, Jul 4, '01
"h:mm a" ----------------------------> 12:08 PM
"hh 'o''clock' a, zzzz" ------------->  12 o'clock PM, Pacific Daylight Time
"K:mm a, z" ------------------------->  0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" ------> 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" -------->  Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" ---------------------> 010704120856-0700                   

Wednesday, July 29, 2009

Blackberry POST Request To the Server with Header information

public static String ProcessPOSTRequest(String serviceUrl, String authToken, String requestData)
{
HttpConnection hc = null ;
        OutputStream os = null;
        
try {
hc = (HttpConnection) Connector.open(serviceUrl,Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content-Type", "text/xml");
//hc.setRequestProperty("Content-Language", "en-US");
//hc.setRequestProperty("Accept", "application/octet-stream");
if (authToken.length()>0)
{
byte[] ByteArray=authToken.getBytes("UTF-8");
String Encoded = Base64OutputStream.encodeAsString(ByteArray, 0, ByteArray.length, false, false);
hc.setRequestProperty("Authorization",Encoded);
}
os = hc.openOutputStream();
os.write(requestData.getBytes(),0,requestData.getBytes().length);
os.flush();
os.close();
int responseCode = hc.getResponseCode();
System.out.println(responseCode);
System.out.println(hc.getResponseMessage());
InputStream inputStream = hc.openInputStream();
StringBuffer sb = new StringBuffer();
int C;
while( -1 != (C = inputStream.read())) 
{
sb.append((char)C);
}
return sb.toString();
} catch (Exception e) {
Dialog.alert(e.getMessage());
}
return null;
}

BackBerry GET Request to the Server

public static String getHttpResponce (String URL) {
try {
HttpConnection httpConnection = (HttpConnection) Connector.open(URL);
httpConnection.setRequestMethod(HttpConnection.GET);
InputStream inputStream = httpConnection.openInputStream();
StringBuffer sb = new StringBuffer();
int C;
while( -1 != (C = inputStream.read())) 
{
sb.append((char)C);
}
return sb.toString();

} catch (Exception e) {
return "-1";
}
}

Tuesday, July 28, 2009

Connect to the Internet Using Blackberry Simulator , Using Eclips

  1. Install Eclips Plugins for Blackberry Development
  2. Goto eclipse->plugins->net.rim.eide.componentpack4.5.0_4.5.0.16->components->MDS
  3. run (click)  run.bat.
  4. Run thr application using eclips blackberry emulator.
  5. Now you can connect to the internaet.

Friday, July 24, 2009

Deploy / Install Application to BlackBerry











  • Select the Alx File and Click next to finish.

Wednesday, July 22, 2009

set Font and Font Size in Blackberry Fields

LabelField lblMsg = new LabelField("");
lblMsg.setFont(getFont().derive(Font.PLAIN, 14));

Tuesday, July 21, 2009

Change Main Screen Background in Blackberry

In here i tried several methods to do that. All methods give me some out put but not perfect. most of examples gives the out put but also gives ugly scoll bar. I used folloeing coding to set main screen backgroung color with out the scoll bar in component pack 4.5.

public class frmLogin extends MainScreen {

VerticalFieldManager vmain;
public frmLogin() 
{
       super(NO_VERTICAL_SCROLL);
      vmain = new VerticalFieldManager(USE_ALL_HEIGHT |USE_ALL_WIDTH)
     {
 protected void paint(net.rim.device.api.ui.Graphics graphics) 
        {
     graphics.setBackgroundColor(0xe6e7e8);  // 0xRRGGBB or Color.Blue
         graphics.clear();
 super.paint(graphics);
        } 
     };
     
     // Add other controlers to vmain
     
     add(vmain);
}

Thursday, July 16, 2009

Connect to web service Blackberry

Assume this as a sample SOAP 1.1 Message

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /Service.asmx HTTP/1.1
Host: 64.49.254.143
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/isValidUser"

><?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>

<isValidUser xmlns="http://tempuri.org/">

<Username>string</Username>

<Password>string</Password>

</isValidUser>

</soap:Body>

</soap:Envelope>

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<isValidUserResponse xmlns="http://tempuri.org/">

<isValidUserResult>boolean</isValidUserResult>

</isValidUserResponse>

</soap:Body>

</soap:Envelope>

------------ Sample Code for Connect to the web service (URL: http://192.21.0.32/Service.asmx) -----------

SoapObject request = new SoapObject("http://tempuri.org/","isValidUser");

request.addProperty("Username","BB");

request.addProperty("Password","BB123");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = request;

envelope.dotNet = true;

HttpTransport ht = new HttpTransport("http://64.49.254.143:8080/Service.asmx");

ht.debug = true;

try {

ht.call("http://tempuri.org/isValidUser", envelope);

Dialog.alert("response"+envelope.getResponse().toString());

}

catch (XmlPullParserException ex)

{

    Dialog.alert("ex1 "+ex.toString() );

ex.printStackTrace();

} catch (IOException ex) {

    Dialog.alert("ex2 " +ex.toString());

ex.printStackTrace();

} catch (Exception ex) {

    Dialog.alert("ex3 " + ex.toString());

}

Refere : http://www.blackberryforums.com/developer-forum/155972-blackberry-ksoap2-tutorial.html

Monday, July 13, 2009

Bitmap Field events, Listeners in Blackberry

bmfDeposit = new BitmapField(Bitmap.getBitmapResource("Deposit.png"),FOCUSABLE)
{
 
   protected void onFocus(int direction) {
setBitmap(Bitmap.getBitmapResource("View.png"));
}
protected void onUnfocus() {
setBitmap(Bitmap.getBitmapResource("Deposit.png"));
}
protected boolean trackwheelClick(int status, int time) {
UiApplication.getUiApplication().pushScreen(new frmDepositMain());
return true;
}
protected boolean keyChar(char character, int status, int time) {
if (Characters.ENTER==character) {
UiApplication.getUiApplication().pushScreen(new frmDepositMain());
}
return true;
}
};

Thursday, July 9, 2009

Turn Hibernet On in Windows Vista

Run command prompt (some times you may want to run as administrator) 
Type powercfg /hibernate on

Wednesday, July 8, 2009

get Screen Width and Height in Backberry

net.rim.device.api.system.Display.getWidth()
net.rim.device.api.system.Display.getHeight()

Progress Bar in Black Berry

Editable Progress Bar
GaugeField gaugeField;
gaugeField = new GaugeField ("PROGRESS ", 0, 30, 15, Field.FOCUSABLE);    

Precentage Progress Bar
GaugeField gaugeField2;
gaugeField2 = new GaugeField ("PERCENT ", 0, 30, 15, GaugeField.PERCENT);
 
Normal Progress Bar
GaugeField gaugeField3;
gaugeField3  = new GaugeField ("NO_TEXT ", 0, 30, 15, GaugeField.NO_TEXT); 

Syntax
GaugeField (lable,min,max,start,style);

Tuesday, July 7, 2009

Detect when an image is added or removed in the BlackBerry device file system

import net.rim.device.api.io.file.FileSystemJournal;
import net.rim.device.api.io.file.FileSystemJournalEntry;
import net.rim.device.api.io.file.FileSystemJournalListener;
import net.rim.device.api.ui.component.Dialog;

public class FileExplorerDemoJournalListener implements FileSystemJournalListener  {

private long _lastUSN;
private String path;
public void fileJournalChanged() {
long nextUSN = FileSystemJournal.getNextUSN();
for (long lookUSN = nextUSN - 1; lookUSN >= _lastUSN; --lookUSN) {
FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
if (entry == null) {
   break; 
}
path = entry.getPath();
switch (entry.getEvent()) {
   case FileSystemJournalEntry.FILE_ADDED:
        Dialog.alert(path);
       break;
   case FileSystemJournalEntry.FILE_DELETED:
         Dialog.alert(path);
       break;
       }
}
}
}

---------------------- Main-----------------------------------
import net.rim.device.api.ui.UiApplication;

public class Main extends UiApplication {

public static void main(String[] args) {
Main theMain = new Main();
theMain.enterEventDispatcher();
}
public Main() {
this.addFileSystemJournalListener(new FileExplorerDemoJournalListener());
}

Thursday, July 2, 2009

Error "c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"

Access to the path 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\0ewxf2b0.diw' is denied.
this error occur when trying to build blackberry application using visual studio plugin.

Run Visual Studio using run as admiistrator

this will work.

Wednesday, July 1, 2009

How to Convert Byte[] to File in C#

public void getFile(Byte[] Files) 
{
String LPath ="sample.jpg";

if (File.Exists(LPath ))
File.Delete(LPath );
   }

FileStream fs = new FileStream(LPath , FileMode.Create, FileAccess.ReadWrite, FileShare.Write);
fs.Write(Files, 0, Files.Length);
fs.Flush();
fs.Close();

}