Selasa, 16 Oktober 2012

Penggunaan alert pada j2me

Alert sangat membantu dalam membuat aplikasi mobile karena dengan adanya alert jika terjadi kesalahan maka alert akan mengeluarkan suara.

berikut adalah contoh alert :

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class contoh_Alert extends MIDlet implements CommandListener {
    Display display;
    Form mainForm;
    Command exitCommand = new Command("Keluar", Command.EXIT, 0);
    Command okCommand = new Command("ya", Command.OK, 0);
    Gauge gauge = new Gauge(null, false, 5, 0);
    Command[] commands = {
        new Command("Alarm", Command.OK, 0),
        new Command("Confirmation", Command.OK, 0),
        new Command("Info", Command.OK, 0),
        new Command("Warning", Command.OK, 0),
        new Command("Error", Command.OK, 0),
        new Command("Modal", Command.OK, 0)
    };

    Alert[] alerts = {
        new Alert("Alarm Alert",
                "Contoh jenis Alarm Alert",
                null, AlertType.ALARM),
                new Alert("Confirmation Alert",
                "Contoh jenis Konfirmasi Alert",
                null, AlertType.CONFIRMATION),
                new Alert("Info Alert", "Contoh jenis Info Alert",
                null, AlertType.INFO),
                new Alert("Warning Alert",
                "Contoh jenis Peringatan Alert",
                null, AlertType.WARNING),
                new Alert("Error Alert",
                "Contoh jenis Kesalahan Alert, w/ an 'OK' Command",
                null, AlertType.ERROR),
                new Alert("Modal Alert",
                "Contoh Pemberitahuan modal: timeout = SELAMANYA",
                null, AlertType.ERROR),
    };

    public contoh_Alert(){
        mainForm = new Form("Contoh Alert");
        mainForm.addCommand(exitCommand);
        for (int i=0; i< commands.length; i++){
            mainForm.addCommand(commands[i]);
        }

        mainForm.setCommandListener(this);
        // Menambah sebuah gauge dan menge-set timeout (milliseconds)
        alerts[3].setIndicator(gauge);
        alerts[3].setTimeout(5000);
        // Menambah sebuah command untuk Alert
        alerts[4].addCommand(okCommand);
        // Menge-Set alert
        alerts[5].setTimeout(Alert.FOREVER);
    }

    public void startApp() {
        if (display == null){
            display = Display.getDisplay(this);
            display.setCurrent(mainForm);
        }
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d){
        if (c == exitCommand){
            destroyApp(true);
            notifyDestroyed();
        }

        for (int i=0; i<commands.length; i++){
            if (c == commands[i]){
                display.setCurrent(alerts[i]);
            }
        }
    }
}

Hasil Run Alert :


2 komentar: