Kamis, 09 April 2015

tugas 2 java mobile ummi thaharani lubis

Diposting oleh Borkat Lubis di 02.32
registry

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ummimidlet;

import javax.microedition.io.PushRegistry;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.*;


/**
 * @author OWNER ACER
 */
public class Midletnotifikasipesan extends MIDlet implements CommandListener{
      protected static final String kConnection = "sms://:50000";
    private Display mDisplay;
    private Form mForm;
    private Command mExitCommand;
 
private Command mRegisterCommand, mUnregisterCommand;

    public void startApp() {
        if (mForm == null) {
            mForm = new Form("PushyMIDlet");
            mExitCommand = new Command("Exit", Command.EXIT, 0);
            mRegisterCommand = new Command("Register",
                    Command.SCREEN, 0);
            mUnregisterCommand = new Command("Unregister",
                    Command.SCREEN, 0);
            mForm.addCommand(mExitCommand);
            mForm.addCommand(mRegisterCommand);
            mForm.addCommand(mUnregisterCommand);
            mForm.setCommandListener(this);
            mDisplay = Display.getDisplay(this);
        }
        mDisplay.setCurrent(mForm);
        try {
            String[] connections;
            connections = PushRegistry.listConnections(false);
            if (connections.length > 0) {
                mForm.append("Registered connections:");
                for (int i = 0; i < connections.length; i++) {
                    mForm.append(connections[i]);
                }
            }
            connections = PushRegistry.listConnections(true);
            if (connections.length > 0) {
// We know there should be only one.
                mForm.append("Connections waiting:");
                for (int i = 0; i < connections.length; i++) {
                    mForm.append(" " + connections[i]);
                }
            }
        } catch (Exception e) {
            mForm.append(e.toString());
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mExitCommand) {
            destroyApp(true);
            notifyDestroyed();
       
}
else if (c == mRegisterCommand) {
            RegisterRunnable rr = new RegisterRunnable(mForm);
            Thread t = new Thread(rr);
            t.start();
        } else if (c == mUnregisterCommand) {
            UnregisterRunnable ur = new UnregisterRunnable(mForm);
            Thread t = new Thread(ur);
            t.start();
        }
    }
}

class RegisterRunnable implements Runnable {

    private Form mForm;

    public RegisterRunnable(Form f) {
        mForm = f;
    }

    public void run() {
        try {
            PushRegistry.registerConnection(
                    MidletNotifikasiPesan.kConnection, "MidletNotifikasiPesan", "*");
            mForm.append("Registered!");
        } catch (Exception e) {
            mForm.append(e.toString());
        }
    }
}

class UnregisterRunnable implements Runnable {

    private Form mForm;

    public UnregisterRunnable(Form f) {
        mForm = f;
    }

    public void run() {
        try {
            PushRegistry.unregisterConnection(
                 MidletNotifikasiPesan.kConnection);
            mForm.append("Unregistered!");
        } catch (Exception e) {
            mForm.append(e.toString());
        }

    }
}

menerima

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package umimidlet;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;


/**
 * @author OWNER ACER
 */
public class Midletumi extends MIDlet implements Runnable, CommandListener {

 private TextBox tb;
 private String text = "Menunggu pesan...";
 private Command okComand;

 public Midletumi() {

  tb = new TextBox("Hello MIDlet", text, 150, 0);

 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 }

 protected void pauseApp() {
 }

 protected void startApp() throws MIDletStateChangeException {
  Display.getDisplay(this).setCurrent(tb);
  new Thread(this).start();

 }

 public void changeScreen() {
  tb = new TextBox("SMS MIDlet", text, 150, 0);
  Display.getDisplay(this).setCurrent(tb);
 }

 public void addConfirmation(String msg) {
  tb = new TextBox("SMS MIDlet", text, 150, 0);
  tb.setString(msg);
  okComand = new Command("OK",Command.OK,1);
  tb.addCommand(okComand);
  tb.setCommandListener(this);
  Display.getDisplay(this).setCurrent(tb);
 }

 public void run() {
  try {
   MessageConnection rconn = (MessageConnection) Connector
     .open("sms://:1234");

   while (true) {
    Message msg = rconn.receive();
    if (msg instanceof TextMessage) {
     TextMessage tmsg = (TextMessage) msg;
     String msgOut = tmsg.getPayloadText();
   
     addConfirmation(msgOut);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void commandAction(Command arg0, Displayable arg1) {
 }

}

0 komentar:

Posting Komentar

 

Ummi Thaharani Lubis Copyright © 2010 Design by Ipietoon Blogger Template Graphic from Enakei