www.pudn.com > 应用RMS实现用户自动登陆功能.rar > LoginMIDlet.java


/* 
 * Created on 2004-11-4 
 * 
 * TODO To change the template for this generated file go to 
 * Window - Preferences - Java - Code Style - Code Templates 
 */ 
package com.j2medev.autologin; 
 
import javax.microedition.lcdui.*; 
import javax.microedition.midlet.MIDlet; 
import javax.microedition.midlet.MIDletStateChangeException; 
 
/** 
 * @author P2800 
 *  
 * TODO To change the template for this generated type comment go to Window - 
 * Preferences - Java - Code Style - Code Templates 
 */ 
public class LoginMIDlet extends MIDlet implements CommandListener 
{ 
 
    private Display display; 
    private Form loginForm; 
    private Form successForm; 
    private TextField userName; 
    private TextField password; 
    private ChoiceGroup autoLogin; 
    private Model model; 
    public static final Command ConnCMD = new Command("Connect", Command.OK, 1); 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see javax.microedition.midlet.MIDlet#startApp() 
     */ 
    protected void startApp() throws MIDletStateChangeException 
    { 
        initMIDlet(); 
        Preference p = model.getPreference(2); 
        if (p == null || !p.isAutoLogin()) 
        { 
            display.setCurrent(loginForm); 
        } else if (p.isAutoLogin()) 
        { 
            Account account = model.getAccount(1); 
            System.out.println("username:" + account.getUsrName() + "password:" 
                    + account.getPassword()); 
            display.setCurrent(successForm); 
 
        } 
 
    } 
 
    public void initMIDlet() 
    { 
        model = new Model(); 
        display = Display.getDisplay(this); 
        loginForm = new Form("LoginForm"); 
        userName = new TextField("username", null, 20, TextField.ANY); 
        password = new TextField("password", null, 20, TextField.PASSWORD); 
        autoLogin = new ChoiceGroup("AutoLogin", Choice.MULTIPLE, 
                new String[] { "remember me" }, null); 
        loginForm.append(userName); 
        loginForm.append(password); 
        loginForm.append(autoLogin); 
        loginForm.addCommand(ConnCMD); 
        loginForm.setCommandListener(this); 
        successForm = new Form("OK"); 
        successForm.append("Ok you have connected to server"); 
    } 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see javax.microedition.midlet.MIDlet#pauseApp() 
     */ 
    protected void pauseApp() 
    { 
        // TODO Auto-generated method stub 
 
    } 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean) 
     */ 
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException 
    { 
        // TODO Auto-generated method stub 
 
    } 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, 
     *      javax.microedition.lcdui.Displayable) 
     */ 
    public void commandAction(Command arg0, Displayable arg1) 
    { 
        String _userName; 
        String _password; 
        boolean auto = false; 
 
        if (arg0 == ConnCMD) 
        { 
            _userName = userName.getString(); 
            _password = password.getString(); 
            auto = autoLogin.isSelected(0); 
            System.out.println(_userName + _password + auto); 
            if (auto) 
            { 
                Account account = new Account(_userName, _password); 
                model.saveAccount(account); 
                Preference preference = new Preference(auto); 
                model.savePreference(preference); 
 
            } 
            display.setCurrent(successForm); 
        } 
    } 
 
}