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();

No comments:

Post a Comment

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...