Wednesday, October 28, 2009

Programmatically Read JAD file in Blackberry

in here following code worked but you have to deploy the application using JAD file (download through the web)  not the using alx file.

public static String getJADProperty(String Name){
    CodeModuleGroup[] allGroups = CodeModuleGroupManager.loadAll();
    CodeModuleGroup myGroup = null;
    String moduleName="";
    moduleName = ApplicationDescriptor.currentApplicationDescriptor().getModuleName();
    for (int i = 0; i < allGroups.length; i++) {
    if (allGroups[i].containsModule(moduleName)) {
    myGroup = allGroups[i];
    break;
    }
    }
 
    // Get the property
    String prop = myGroup.getProperty(Name);
    return prop;
}

You can get the custom property by calling



// You can get the MyTag:something
String value = getJADProperty("MyTag")

Tuesday, October 27, 2009

Asynchronous Threading in Blackberry

clip_image001[10]

Assume that you are having a class called Class A it needs to call Class Z as a thread. Therefore you can  implement class Z from Runnable interface and implement your code in run().

Ex:-
public class ClassZ implements Runnable {
 
    public static final int METHOD1 =1;
    public static final int METHOD2= 2;    
 
    int code;
    
    public ClassZ(int code)
    { this.code=code; }     
 
    private void method1()
    { //Code for Method1 }
 
    private void method2()
    { //Code for Method2 }
 
    public void run() {
    
        switch (code)
        {
        case ClassZ.METHOD1 :
            method1();
            break;
        case ClassZ.METHOD2:
            method2();
            break;
        }
    }
 
    public void runThread() {
    thread = new Thread(this);
    thread.start();
    }
}


If you start the Thread using ClassZ it runs ClassZ’s run method. Therefore  if you have several method in side the class you need to have a variable telling which method you want to run. Thus in here i used code variable to  keep the information about the method which i want to run. And also we wrote a method to start the thread called runThread(). 





public Class ClassA 
{
    method()
    {
        // Initialize the ClassZ by passing which
        // method we used to invoke as thread
        ClassZ clsz = new ClassZ(ClassZ.method1);
        clsz.runThread();
    }
}


Sometimes we want to do something after finish executing the thread. But in here there are no return vales in a thread. therefore we use mechanism called callBack().










 clip_image001[12]
Assume that you are calling the thread class  from ClassA or ClassB and want to get some feedback according the caller. Therefore we can change the code as below.



public class ClassZ implements Runnable {
 
    public static final int METHOD1 =1;
    public static final int METHOD2= 2;    
 
    int code;
    Object object;
    
    public ClassZ(int code,Object object)
    { this.code=code;
      this.object=object;  
    }     
 
    private void method1()
    { //Code for Method1
        callBack();
    }
 
    private void method2()
    { //Code for Method2 
        callBack();
    }
 
    public void run() {
    
        switch (code)
        {
        case ClassZ.METHOD1 :
            method1();
            break;
        case ClassZ.METHOD2:
            method2();
            break;
        }
    }
 
    public void runThread() {
    thread = new Thread(this);
    thread.start();
    }
 
    void callBack()
    {
        if (object instanceof ClassA) {
            ((ClassA) object).callBack();
        }
        
        else if (object instanceof ClassB) {
            ((ClassB) object).callBack();
        }
    }
}
If method finished executed it invoke callBack(). The method check the object instance and invoke relevant callBack() in the class.



public Class ClassA 
{
    method()
    {
        // Initialize the ClassZ by passing which
        // method we used to invoke as thread and 
        // object itself
        ClassZ clsz = new ClassZ(ClassZ.method1,this);
        clsz.runThread();
    }
 
    public void callBack()
    {
        // code after executing the
        // thread
    }
}

Thursday, October 22, 2009

Best Practices in programming

I though to write a continuous article describing best practices in programming. Here I’m not going to talking about  coding conventions but talking about enhanced cording techniques you can use.

Using IO, Network connections

InputStream streamRead =null;
try {
    //  Use InputStream to do your work   
} catch (Exception ex) {
    // Exception Handling
}
finally
{
    try {
    // Try to close the stream, This may also give erros
    // resulting opened stream
    streamRead.close();
    } catch (IOException e) {
    }
    // Set Stream to null to prevent
    // unwanted openings
    streamRead=null;
}

Not to use methods in loop’s conditions

// This is not optimized
for (int i = 0; i < elements.capacity(); ++i)
{
    // Code Here     
}
 
// This is optimized,
// elements.capacity() only call in one time
int capacity = elements.capacity();
for (int i = 0; i < capacity; ++i)
{
    // Code Here  
}

Use String Buffer wherever possible.

// This will create two string and
// append it
String str = new String ("My Name is : ");
str += "Mleick";
 
// This is better
StringBuffer str = new StringBuffer ("My Name is :");
str.append("Mleick!!");

Use Threads to call server calls where ever possible.


This prevents user to feel as hang the system during the network calls. I’ll write a article including asynchronous server calls.   

Tuesday, October 6, 2009

get Blackberry Device ID Programmatically

// import net.rim.device.api.system.DeviceInfo
DeviceInfo.getDeviceId();