黑莓 - 运行后台应用程序

发布于 2024-08-30 23:51:43 字数 4201 浏览 13 评论 0原文

下午好。 我开始用java和黑莓编程。 我正在开发一个具有三个窗口的应用程序,我将在其中显示有关设备的基本信息,这些窗口已经完成并正在运行。 我需要创建一个在后台运行的进程,该进程每 10 分钟运行一次。 当我让这个进程在后台运行并正在努力关闭窗口时?

这是运行应用程序的类:

    public class InfoBerry extends UiApplication{
    public vtnprincipal vtnprincipal;
    public vtnbateria vtnbateria;
    public vtnestado vtnestado ;
    public vtnacerca vtnacerca;

    public InfoBerry(){

    }

    public static void main(String[] args) {
            InfoBerry theApp = new InfoBerry();
            theApp.mostrarpantalla();
    }
    public void mostrarpantalla(){

        vtnprincipal = new vtnprincipal(this);
        vtnbateria = new vtnbateria(this);
        vtnestado = new vtnestado(this);
        vtnacerca = new vtnacerca(this);
        // Inicailizamos los componentes de la pantalla principal
        vtnprincipal.incventana();
        // La pnemos en lo alto de la pila de pantallas

        pushScreen(this.vtnprincipal);
        }
}

这是您需要在后台运行的类。 因为我必须调用此类才能在后台运行?

class iconnoti extends MainScreen{
    //icono de la temperatura
    EncodedImage imgtem = 
            EncodedImage.getEncodedImageResource("icon_bateria_t.png");
    ApplicationIcon icontem = new ApplicationIcon(imgtem);
  //icono de la carga de la bateria
    EncodedImage imgcarga = 
            EncodedImage.getEncodedImageResource("icon_bateria.png");
    ApplicationIcon iconcarga = new ApplicationIcon(imgcarga);
    //icono de la memoria
    EncodedImage imgmemo = 
            EncodedImage.getEncodedImageResource("icon_memoria.png");
    ApplicationIcon iconmemo = new ApplicationIcon(imgmemo);
    ApplicationIcon mIcon = icontem;
    boolean act;
    public iconnoti() {


    }

    public void rotar_temperatura(){
        cron c1;
        actualizar_icono(icontem);
        actualizar_valor(DeviceInfo.getBatteryTemperature());

        c1 = new cron(2,10000);
        c1.start();


    }
    public void rotar_memoria(){
        cron c1;
        actualizar_icono(iconmemo);
        actualizar_valor(34);

        c1 = new cron(3,10000);
        c1.start();

    }
    public void rotar_nivel(){
        cron c1;
        actualizar_icono(iconcarga);
        actualizar_valor(DeviceInfo.getBatteryLevel());
        c1 = new cron(1,10000);
        c1.start();

    }

   public void iniciar_servicio() {
       try {

           ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator Indicator = 
                    reg.register(mIcon, false, true);
                } catch (Exception e) {

        }
    }

    public  void parar_servicio() {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                reg.unregister();
        } catch (Exception e) {
        }
    }

    void actualizar_valor(int value) {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator appIndicator = 
                    reg.getApplicationIndicator();
                appIndicator.setValue(value);
        } catch (Exception e) {
        }
    }

    void actualizar_icono(ApplicationIcon icon) {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator appIndicator = 
                    reg.getApplicationIndicator();
                appIndicator.setIcon(icon);
        } catch (Exception e) {
        }
    }
}

class cron extends Thread {
    //private ApplicationIcon icono;
    public int valor;
    private int tiempo;
    iconnoti icon = new iconnoti();

    public cron(int v, int t){
        valor = v;
        tiempo = t;
    }

    public void run() {
        try {
            sleep(tiempo);
        } catch (InterruptedException e) {  

        }
        if(valor == 1){
            icon.rotar_temperatura();
        }else if(valor == 2){
            icon.rotar_memoria();
        }else if(valor == 3){
            icon.rotar_nivel();
        }

    }           

}

感谢您的帮助。

Good afternoon.
I'm starting programming in java and blackberry.
I am developing an application with three windows, which I will show basic information about the device, these windows are already done and working.
I need to create a process running in the background, this process will run every 10 minutes.
As I make this process run in the background and is working to close the windows?

This is the kind that runs the application:

    public class InfoBerry extends UiApplication{
    public vtnprincipal vtnprincipal;
    public vtnbateria vtnbateria;
    public vtnestado vtnestado ;
    public vtnacerca vtnacerca;

    public InfoBerry(){

    }

    public static void main(String[] args) {
            InfoBerry theApp = new InfoBerry();
            theApp.mostrarpantalla();
    }
    public void mostrarpantalla(){

        vtnprincipal = new vtnprincipal(this);
        vtnbateria = new vtnbateria(this);
        vtnestado = new vtnestado(this);
        vtnacerca = new vtnacerca(this);
        // Inicailizamos los componentes de la pantalla principal
        vtnprincipal.incventana();
        // La pnemos en lo alto de la pila de pantallas

        pushScreen(this.vtnprincipal);
        }
}

And this is the class you need to run in the background.
As I have to make the call to this class to run in the background?

class iconnoti extends MainScreen{
    //icono de la temperatura
    EncodedImage imgtem = 
            EncodedImage.getEncodedImageResource("icon_bateria_t.png");
    ApplicationIcon icontem = new ApplicationIcon(imgtem);
  //icono de la carga de la bateria
    EncodedImage imgcarga = 
            EncodedImage.getEncodedImageResource("icon_bateria.png");
    ApplicationIcon iconcarga = new ApplicationIcon(imgcarga);
    //icono de la memoria
    EncodedImage imgmemo = 
            EncodedImage.getEncodedImageResource("icon_memoria.png");
    ApplicationIcon iconmemo = new ApplicationIcon(imgmemo);
    ApplicationIcon mIcon = icontem;
    boolean act;
    public iconnoti() {


    }

    public void rotar_temperatura(){
        cron c1;
        actualizar_icono(icontem);
        actualizar_valor(DeviceInfo.getBatteryTemperature());

        c1 = new cron(2,10000);
        c1.start();


    }
    public void rotar_memoria(){
        cron c1;
        actualizar_icono(iconmemo);
        actualizar_valor(34);

        c1 = new cron(3,10000);
        c1.start();

    }
    public void rotar_nivel(){
        cron c1;
        actualizar_icono(iconcarga);
        actualizar_valor(DeviceInfo.getBatteryLevel());
        c1 = new cron(1,10000);
        c1.start();

    }

   public void iniciar_servicio() {
       try {

           ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator Indicator = 
                    reg.register(mIcon, false, true);
                } catch (Exception e) {

        }
    }

    public  void parar_servicio() {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                reg.unregister();
        } catch (Exception e) {
        }
    }

    void actualizar_valor(int value) {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator appIndicator = 
                    reg.getApplicationIndicator();
                appIndicator.setValue(value);
        } catch (Exception e) {
        }
    }

    void actualizar_icono(ApplicationIcon icon) {
        try {
                ApplicationIndicatorRegistry reg = 
                    ApplicationIndicatorRegistry.getInstance();
                ApplicationIndicator appIndicator = 
                    reg.getApplicationIndicator();
                appIndicator.setIcon(icon);
        } catch (Exception e) {
        }
    }
}

class cron extends Thread {
    //private ApplicationIcon icono;
    public int valor;
    private int tiempo;
    iconnoti icon = new iconnoti();

    public cron(int v, int t){
        valor = v;
        tiempo = t;
    }

    public void run() {
        try {
            sleep(tiempo);
        } catch (InterruptedException e) {  

        }
        if(valor == 1){
            icon.rotar_temperatura();
        }else if(valor == 2){
            icon.rotar_memoria();
        }else if(valor == 3){
            icon.rotar_nivel();
        }

    }           

}

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-09-06 23:51:43

后台应用程序是一种进程,因此至少在开始时没有GUI。

  • 您应该扩展 Application 而不是 UIApplication 类
  • 您不应该将屏幕推到那里,只需将所有内容从 iconnoti 类移动到 cron 类并在应用程序构造函数中运行它:

    public class BerryInfoApp 扩展了 Application {
        公共 BerryInfoApp() {
                UpdateThread updateThread = new UpdateThread(10*60*1000);
                updateThread.run();
        }
        公共静态无效主(字符串[] args){
            (new BerryInfoApp()).enterEventDispatcher();
        }
    }
    
    类 UpdateThread 扩展 Thread {
    编码图像 imgtem = 编码图像
            .getEncodedImageResource("icon_bateria_t.png");
    ApplicationIcon icontem = new ApplicationIcon(imgtem);
    编码图像 imgcarga = 编码图像
            .getEncodedImageResource("icon_bateria.png");
    ApplicationIcon iconcarga = new ApplicationIcon(imgcarga);
    编码图像 imgmemo = 编码图像
            .getEncodedImageResource("icon_memoria.png");
    ApplicationIcon iconmemo = new ApplicationIcon(imgmemo);
    ApplicationIcon mIcon = icontem;
    
    静态最终 int ACTION_NONE = 0;
    静态最终 int ACTION_BATTERY_TEMP = 1;
    静态最终 int ACTION_MEMORY = 2;
    静态最终 int ACTION_BATTERY_LEVEL = 3;
    int mAction = ACTION_BATTERY_LEVEL;
    长周期;
    
    公共 UpdateThread(int 周期) {
        mPeriod = 周期;
    }
    
    公共无效停止(){
        mAction = ACTION_NONE;
    }
    
    公共无效运行(){
        iniciar_servicio();
        while (mAction != ACTION_NONE) {
            开关(mAction){
            案例ACTION_BATTERY_TEMP:
                旋转温度();
                mAction = ACTION_MEMORY;
                休息;
            案例ACTION_MEMORY:
                旋转记忆();
                mAction = ACTION_BATTERY_LEVEL;
                休息;
            案例ACTION_BATTERY_LEVEL:
                rotar_nivel();
                mAction = ACTION_BATTERY_TEMP;
                休息;
            默认:
                休息;
            }
            尝试 {
                睡眠(mPeriod);
            } catch (InterruptedException e) {
            }
        }
        parar_servicio();
    }
    
    公共无效 rotar_temperatura() {
        actualizar_icono(icontem);
        currentizar_valor(DeviceInfo.getBatteryTemperature());
    }
    
    公共无效 rotar_memoria() {
        actualizar_icono(iconmemo);
        实际值(34);
    }
    
    公共无效 rotar_nivel() {
        actualizar_icono(iconcarga);
        actualizar_valor(DeviceInfo.getBatteryLevel());
    }
    
    公共无效 iniciar_servicio() {
        尝试 {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator 指标 = reg.register(mIcon, false, true);
        } catch (异常 e) {
        }
    }
    
    公共无效 parar_servicio() {
        尝试 {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            reg.unregister();
        } catch (异常 e) {
        }
    }
    
    无效actualizar_valor(int值){
        尝试 {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator appIndicator = reg.getApplicationIndicator();
            appIndicator.setValue(值);
        } catch (异常 e) {
        }
    }
    
    无效actualizar_icono(应用程序图标图标){
        尝试 {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator appIndicator = reg.getApplicationIndicator();
            appIndicator.setIcon(图标);
        } catch (异常 e) {
        }
    }
    }
    

Background Application is a kind of process, so there is no GUI at least on the beginning.

  • You should extend Application instead of UIApplication class
  • You should not push screen there, just move everything from iconnoti class to cron class and run it in Application constructor:

    public class BerryInfoApp extends Application {
        public BerryInfoApp() {
                UpdateThread updateThread = new UpdateThread(10*60*1000);
                updateThread.run();
        }
        public static void main(String[] args) {
            (new BerryInfoApp()).enterEventDispatcher();
        }
    }
    
    class UpdateThread extends Thread {
    EncodedImage imgtem = EncodedImage
            .getEncodedImageResource("icon_bateria_t.png");
    ApplicationIcon icontem = new ApplicationIcon(imgtem);
    EncodedImage imgcarga = EncodedImage
            .getEncodedImageResource("icon_bateria.png");
    ApplicationIcon iconcarga = new ApplicationIcon(imgcarga);
    EncodedImage imgmemo = EncodedImage
            .getEncodedImageResource("icon_memoria.png");
    ApplicationIcon iconmemo = new ApplicationIcon(imgmemo);
    ApplicationIcon mIcon = icontem;
    
    static final int ACTION_NONE = 0;
    static final int ACTION_BATTERY_TEMP = 1;
    static final int ACTION_MEMORY = 2;
    static final int ACTION_BATTERY_LEVEL = 3;
    int mAction = ACTION_BATTERY_LEVEL;
    long mPeriod;
    
    public UpdateThread(int period) {
        mPeriod = period;
    }
    
    public void stop() {
        mAction = ACTION_NONE;
    }
    
    public void run() {
        iniciar_servicio();
        while (mAction != ACTION_NONE) {
            switch (mAction) {
            case ACTION_BATTERY_TEMP:
                rotar_temperatura();
                mAction = ACTION_MEMORY;
                break;
            case ACTION_MEMORY:
                rotar_memoria();
                mAction = ACTION_BATTERY_LEVEL;
                break;
            case ACTION_BATTERY_LEVEL:
                rotar_nivel();
                mAction = ACTION_BATTERY_TEMP;
                break;
            default:
                break;
            }
            try {
                sleep(mPeriod);
            } catch (InterruptedException e) {
            }
        }
        parar_servicio();
    }
    
    public void rotar_temperatura() {
        actualizar_icono(icontem);
        actualizar_valor(DeviceInfo.getBatteryTemperature());
    }
    
    public void rotar_memoria() {
        actualizar_icono(iconmemo);
        actualizar_valor(34);
    }
    
    public void rotar_nivel() {
        actualizar_icono(iconcarga);
        actualizar_valor(DeviceInfo.getBatteryLevel());
    }
    
    public void iniciar_servicio() {
        try {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator Indicator = reg.register(mIcon, false, true);
        } catch (Exception e) {
        }
    }
    
    public void parar_servicio() {
        try {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            reg.unregister();
        } catch (Exception e) {
        }
    }
    
    void actualizar_valor(int value) {
        try {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator appIndicator = reg.getApplicationIndicator();
            appIndicator.setValue(value);
        } catch (Exception e) {
        }
    }
    
    void actualizar_icono(ApplicationIcon icon) {
        try {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                    .getInstance();
            ApplicationIndicator appIndicator = reg.getApplicationIndicator();
            appIndicator.setIcon(icon);
        } catch (Exception e) {
        }
    }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文