Android Menuの表示

Menuの表示です。

public class MainActivity extends Activity {

	private static final int MENU_DEBUG_LOG = Menu.FIRST + 1;
	
	public boolean onCreateOptionMenu(Menu menu) {
		
		menu.add(Menu.NONE,MENU_DEBUG_LOG,Menu.NONE,"OpenLog");
		
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		
		boolean ret = true;
		
		switch (item.getItemId()) {
		case MENU_DEBUG_LOG:
			break;
		}
		
		return ret;
		
		
	}

}

Android Dialogの表示

AlertDialogの表示方法です。

public class MainActivity extends Activity {

    public void showDialog() {

		nameText = new EditText(this);
		nameText.setHint("Enter Your Name.");
		nameText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		nameText.setMaxLines(1);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Enter Your Name?");
		builder.setView(nameText);
		builder.setCancelable(false);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {

		        	   dialog.cancel();
		        	   String nickname = nameText.getText().toString();
		        	   PlayerInfo.regisetNickName(nickname);
		        	   registScoreRanking();
		        	   moveToScoreRanking();
		        	   

		           }
		       });
		builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });

		AlertDialog alert = builder.create();
		alert.show();

    }

Java5 JMX

Java5でJMXに接続してメモリ消費量を調査する必要がでてきたので、JMXに関してのメモです。
※書く途中です。

import java.io.IOException;

import sun.management.ConnectorAddressLink;


public class JmxTest {

	public static void main(String[] args) {
		
		int pid = Integer.parseInt(args[0]);
		try {
			String address = ConnectorAddressLink.importFrom(pid);
			
			//接続つくってとか。。。
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
}

rt.jar、tools.jarをビルドパスに入れておきましょう。
Eclipseの設定で、
window->Preference->Compiler->Error/Warning->Deprecate and Restricted API->Forbidden Reference
をWarningに変更しておく。

Python SMTP通信

Python勉強中です。
SMTPとの会話をするプログラムを作成したので、リマインダー代わりに記載しておきます。
※テストプログラムなのでエラー処理とかしてません。

import socket

class SMTP:
    
    def __init__(self):
        self.socket = None
        
    def connect(self,host) :
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.connect((host,25))
        self.s = s

    def helo(self) :
        self.s.sendall("HELO localhost\r\n")

    def close(self):
        self.s.shutdown(socket.SHUT_RDWR)

    def mail_from(self,from_address) :
        self.s.sendall("MAIL FROM: " + from_address + "\r\n")
        line = self.readLine()
        if line.find("250") > -1:
            return True
        return False

    def rcpt_to(self,to_address) :
        self.s.sendall("RCPT TO: " + to_address + "\r\n")
        line = self.readLine()
        if line.find("250") > -1:
            return True
        return False

    def data(self,data) :
        self.s.sendall("DATA\r\n")
        line = self.readLine()
        if line.find("354") < 0:
            return False
        self.s.sendall(data+"\r\n.\r\n")
        line = self.readLine()
        if line.find("250") > -1:
            return True
        return False

    def readLine(self) :
        result = ""
        while True:
            code = self.s.recv(1)
            if (code == "\r"):
                code_next = self.s.recv(1)
                if (code_next == "\n"):
                    break
                result += code + code_next
            else :
                result += code
        
        return result
    

x = SMTP()
x.connect("localhost")
print(x.readLine())
x.helo()
print(x.readLine())
if x.mail_from("root@localhost"):
    print("FROM OK")
if x.rcpt_to("root@localhost"):
    print("RCPT OK")
if x.data("test"):
    print("DATA OK")

x.close()

まだ、ちょっとしたプログラムしか組んでないですが、Python楽しいですね。

Python ソケット通信

Pythonの勉強をしよう!!
と、思いつつも、仕事ではJavaPHPRubyがメイン。

Pythonを使う機会がないですね。。。
ちょっとしたプログラムだったらPHPで作ってしまうのですが、やはりできるだけPythonを利用したい。
とはいえ、時間のない業務の中でぱっと作るときにはなれた言語で作ってしまうもの。
なので、ちょっとしたPHPプログラムをPythonで書くことによって勉強しようと模索してみる。

最近はメール関連の仕事なので、簡単にメールを送るテストプログラムを。
まずはソケット開いてHELOするまで。

<?

class SMTP {
      
      private $socket;
      
      public function connect($host) {
             $this->socket = fsockopen($host,25);
      }

      public function close() {
             fclose($this->socket);
      }

      public function helo() {
             fwrite($this->socket,"HELO localhost\r\n");
      }

}


$class = new SMTP();
$class->connect("localhost");
$class->helo();
$class->close();

?>

これをPythonでかくとこんな感じ。

import socket

class SMTP:
    
    def __init__(self):
        self.socket = None
        
    def connect(self,host) :
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.connect( (host,25) )
        self.s = s

    def helo(self) :
        self.s.sendall("HELO localhost\r\n")

    def close(self):
        self.s.shutdown(socket.SHUT_RDWR)

x = SMTP()
x.connect("localhost")
x.helo()
x.close()