2010-05-04 162 views
2

下午好。 我開始用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(); 
     } 

    }   

} 

感謝您的幫助。

回答

3

背景應用程序是一種進程,所以在開始時至少沒有GUI。

  • 你應該擴大,而不是UIApplication類應用
  • 你不應該推屏幕出現,只需將一切從iconnoti類與cron類,並在應用程序構造運行它:

    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) { 
        } 
    } 
    } 
    
+0

感謝您的迴應和幫助,我正在實施代碼,並在完成評論時發佈。 謝謝。 – Leandro 2010-05-06 00:08:38

+0

不客氣! – 2010-05-06 03:44:03