Display progressing bar in BlackBerry/Run and stop thread from 2 different classes/Use of volatile threads

 

 

package com.ironone.thread;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.DialogFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

public class service implements Runnable
{
    private volatile Thread service;
    private int maximum, timeout;
    private boolean useful;
    private PopupScreen popup;
    private GaugeField gaugeField;
    public service(int maximum, int timeout,String title)
    {
            this.maximum = maximum;
            this.timeout = timeout;
            DialogFieldManager manager = new DialogFieldManager();
            popup = new PopupScreen(manager);
            gaugeField = new GaugeField(null, 1, maximum, 1, GaugeField.NO_TEXT);
            manager.addCustomField(new LabelField(title));
            manager.addCustomField(gaugeField);       
            service = new Thread(this);
            service.start();
    }
    public void run()
    {
        Thread thisThread = Thread.currentThread();
        while ( service == thisThread )
        {            useful = true;
            UiApplication.getUiApplication().invokeLater(new Runnable()
            {
                public void run()
                {
                    UiApplication.getUiApplication().pushScreen(popup);
                }
            });
            int iterations = 0;
            while (useful)
            {
                try
                {
                    Thread.sleep(timeout);
                }
                catch (Exception e)
                {
                }
                if (++iterations > maximum)
                {
                    useful =false;
                    iterations = 1;
                }
                gaugeField.setValue(iterations);
            }

            if (popup.isDisplayed())
            {
                UiApplication.getUiApplication().invokeLater(new Runnable()
                {
                    public void run()
                    {
                        UiApplication.getUiApplication().popScreen(popup);
                    }
                });
            }
        }
    }

    public synchronized void serviceResume()
    {
        service = new Thread(this);
        service.start();
    }
    public synchronized void serviceSuspend()
    {
        service = null;
    }

}

Create class to use thread

package com.ironone.thread;
import com.ironone.thread.service;

public class dta
{
    public boolean temp=true;
    public service eeeeee=null;//new service(10,10,"Display name");
    public void dta()
    {
        eeeeee=new service(50,50,"Sending");
    }
    public void sss()
    {
        this.eeeeee.serviceSuspend();
    }
}

 

Create a global variable of dta in main class

public static dta ss=new dta();

refer that variable from anywhere and start run

Main.ss.dta();

stop it from some other location or class

Main.ss.sss();

Post method in java

public String postObject(String url,String object)
       {

          String resultsPage="";
         try{
              String urlStr=url;
              String val=object;
              URL postURL = new URL( urlStr );
              HttpURLConnection con = (HttpURLConnection) postURL.openConnection();
              con.setUseCaches(false);      
              con.setDoOutput(true);        
              con.setDoInput(true);         
              con.setRequestMethod("POST");  
              PrintWriter out = new PrintWriter(con.getOutputStream());
              String postStr = "link="+URLEncoder.encode(val);
              out.println(postStr); 
              out.close();
              String inputLine="";
        BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream()));
              while ((inputLine = in.readLine()) != null)
                   resultsPage+=inputLine;
              in.close();
              System.out.println(resultsPage); //Final output
          }
         catch(Exception ex)
          {
             System.out.println(ex);
          }

      return resultsPage;
       }

Get properties of Image programmatically / Make bitmap image from file located in any location -BlackBerry Development

Create Bitmap image as follows then

Bitmap _bitmap;
_bitmap=getBitmap("file:///SDCard/image.jpg");
int h=_bitmap.getHeight();
int w=_bitmap.getWidth();
Dialog.alert("height"+Integer.toString(h)+"width"+Integer.toString(w));

getBitmap image will be like this

public Bitmap getBitmap(String filepath)
{
    Bitmap bkBitmap = null;
    try {
             FileConnection fconn = (FileConnection)Connector.open(filepath );
             InputStream input = null;
             input = fconn.openInputStream();     
             int available = 0;
             available = input.available();
             int fSz = (int)fconn.fileSize();
             byte[] data = new byte[fSz];            
             input.read(data, 0, fSz);
             EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);
             bkBitmap = image.getBitmap();               
        }
    catch(Exception e)
        {
        }
    return bkBitmap;
}

“Some of the projects in the active BlackBerry configuration contain errors. Project MobileRDM contains errors (Please check the Problems view). “ or “running the rapc utility error” Errors in eclipse Blackberry development when try to add jar file

There few ways to fix this problem here i have mentioned the method that works for me

01)Select project folder and make sure that is not read only.if it is read only remove

      the read only property

02)In the eclipse project explorer window select project then go to >build path

    >Configure Build path > Then go to > libraries and add the necessary jar files

    from add jars then order and export menu select the added jar file then press ok

Then problem will be ok

Cannot find jar error2 when building BlackBerry Application in eclipse

 

We can see sometimes above error in console

Go to my computer->properties –>Advanced System properties

->Environmental variables->System variables

find path then add “;your java bin path”

Then problem will not occur again.this works for me

Create folder in BlackBerry device memory or Memory card-(How to avoid “FileIOException: File system error”)

Creates Folder inside the BlackBerry device programmatically

try
    {
FileConnection fc=(FileConnection)Connector.open

("file:///store/home/user/themes/",Connector.READ_WRITE);

//Give your path as need above path for device memory

//for Memory Card give as file:///SDCard/Themes/

//Do not forget to “/” at the end of name
           if (!fc.exists())
            {
             fc.mkdir();
             Dialog.alert("Created Folder");
            }
    }
    catch(Exception e)
    {
        Dialog.alert("ERROR");
    }

Download image or any file from URL to specific file path-BlackBerry Development

Give url and downloaad location very simple

public boolean downloadImage(String url,String path)
{
    if(saveImage(getImage(url),path))
    {
    return true;
    }
    else
    {
        return false;
    }
}
public static String getImage(String url)
{
String response = "";
try {
HttpConnection conn = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
InputStream input =conn.openInputStream();
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
while( -1 != (len = input.read(data)))
{
raw.append(new String(data, 0, len));
}
response = raw.toString();
input.close();
} catch(Exception e)
{
Dialog.alert("Error");
}
return response;
}
public boolean saveImage(String txt,String path1)
    {
        boolean str = false;
        try {
            FileConnection fc1 = (FileConnection)Connector.open(path1, Connector.READ_WRITE);
            if(!fc1.exists())
            {
            fc1.create();
            }
            fc1.setWritable(true);
                byte[] size1=txt.getBytes();
                OutputStream os=fc1.openOutputStream();
                os.write(size1);
                os.close();
                return true;        
        }
        catch (Exception ioe)
        {
            System.out.println("File Sys Error"+ioe.getMessage());
        }
        return str;
    }

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...