Blackberry - Running Background Application

最后都变了- 提交于 2019-12-04 15:30:35

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) {
        }
    }
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!