I am developing a j2me application where there is one parent midlet which calls other java programs. Parent midlet is of implicit list which contains 4 elements. On clicking any of the elements an appropriate java program is called. Everything is working fine, but i don't understand how to show parent midlet from java program on clicking of back button.
Please provide examples.
this is my parent midlet code
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class Contacts extends MIDlet implements CommandListener,Runnable {
Display display=null;
private Form form=new Form("Contacts");
private List menu=new List("Contact Menu",Choice.IMPLICIT);
private Command exit = new Command("Exit", Command.EXIT, 2);
private Command ok=new Command("Ok",Command.SCREEN,1);
private Command back = new Command("Back", Command.BACK, 1);
private Alert alert;
public Contacts()
{
display = Display.getDisplay(this);
try{
menu.append("Add Contact", Image.createImage("/contact_new.png"));
menu.append("Delete Contact",Image.createImage("/delete-icon.png"));
menu.append("Get Contact",Image.createImage("/document-edit.png"));
menu.append("View Contacts",Image.createImage("/view.png"));
menu.addCommand(ok);
menu.addCommand(exit);
menu.setCommandListener(this);
}
catch(IOException ie)
{
}
}
public void startApp() {
display.setCurrent(menu);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(true);
notifyDestroyed();
return;
}
switch (menu.getSelectedIndex ()) {
case 0:
new ac (this);//call to java program
break;
case 1:
new dc (this);
break;
case 2:
new gc(this);
break;
case 3:
new vc(this);
break;
default:
System.err.println ("Unexpected choice...");
break;
}
}
}
I don't know any API in standard J2ME to interact between MIDlets. At least that is already released. There is some Nokia private API to do some such kind of operations.
Related
Is it possible to get the value of phobe in-call volume from java me midlet? Changing of the volume is not necessary.
Thanks.
AFAIK,
It is not possible to access the phone volume. But you can set your application volume or get the application.
Sample code for Controlling the volume of your application :
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Ticker;
import javax.microedition.media.*;
public class VolumeControlDemo extends MIDlet implements CommandListener {
private Display display;
private Command exit,incr,decr;
Form frm;
VolumeControl vc;
int vol;
Player player;
public VolumeControlDemo() {
display = Display.getDisplay(this);
}
public void startApp() {
frm=new Form("VolumeControlDemo Demo");
exit= new Command("Exit",Command.EXIT,1);
decr= new Command("Decrease",Command.EXIT,1);
incr= new Command("Increase",Command.EXIT,1);
frm.addCommand(exit);
frm.addCommand(decr);
frm.addCommand(incr);
frm.setCommandListener(this);
display.setCurrent(frm);
try {
// Creating player object
player = Manager.createPlayer("/demo.wav");
// Setting loop count
player.setLoopCount(-1);
// Start sound
player.start();
Control cs[];
// Getting Controls object
cs = player.getControls();
for (int i = 0; i < cs.length; i++) {
if (cs[i] instanceof VolumeControl)
// Getting volume control
vc=(VolumeControl)cs[i];
}
} catch (Exception e) {}
}
public void pauseApp() {
}
public void destroyApp(boolean un) {
}
public void commandAction(Command cmd,Displayable d) {
try {
if(decr) {
if(vol>0) vol--;
vc.setLevel(vol);
} else if() {
if(vol<99) vol--;
vc.setLevel(vol);
}
frm.appent("vol="+vc.getLevel());
}catch(Exception e){}
}
}
I'm working in java me. I'm trying to switch between visual designs using ok Commands and back Commands. I have a form displayable which I named formA in my main class A.java and a formB in another class B.java . I used an ok Command in formA which on selection, is supposed to take the user to formB.
I created a reference to B.java in my main class A.java constructor
B b;
// A.java constructor
public A() {
b = new B(this);
}
now I could call the getFormB method from my commandAction in formA. Then I added a backCommand which is supposed to take me back to formA in A.java and I tried creating a reference in B.java same way I did in A.java but I get a SecurityException MIDletManager ERROR at runtime. I was adviced to add an A attribute to my B class and receive the instance as a constructor parameter so I can call the getFormA() method to switch to formA in A.java
A a;
B(A a) {
this.a = a;
}
in command action I did ds on the backCommand:
switchDisplayable ( null , a.getFormA());
This compiled, but at runtime on hitting the BACK key from formB I get java/lang/NullPointerException.
Can anyone tell me why this happended and how to fix it please. All I'm trying to acheive is the backCommand to take the user back to formA from formB
If your A class extends Form or your A class is Displayable, then in the Back command, you can just tell switchDisplayable(null, a).
If your A class is not a Form, then make sure your A class has the following methods:
public Form getFormA() {
return ...; // return the `Form` here so you will not get NullPointerException
}
UPDATE:
If you're using NetBeans, you can open Flow tab and drag backCommand from formB to formA. NetBeans will generate the required code for you.
If you code by hand, then it will looks like the following:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ExampleMidlet extends MIDlet {
private Display display;
private Form formA;
private Form formB;
private Command formA_next;
private Command formB_back;
public void startApp() {
if (display==null) {
display = Display.getDisplay(this);
formA = new Form("Form A");
formA_next = new Command("Next", Command.SCREEN, 0);
formA.addCommand(formA_next);
formA.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c==formA_next) {
display.setCurrent(formB);
}
}
});
formB = new Form("Form B");
formB_back = new Command("Back", Command.BACK, 0);
formB.addCommand(formB_back);
formB.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c==formB_back) {
display.setCurrent(formA);
}
}
});
}
display.setCurrent(formA);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
I don't know how you code your Form, but it seems that a is null. Maybe you can show me the full code. Passing this in constructor is generally not recommended. By the way, you still need a 'main' class that extends MIDlet right? Then there will be 3 classes, such as:
ExampleMiddlet.java (this is where you put your MIDlet lifecycle, such as startApp(), pauseApp(), etc):
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ExampleMidlet extends MIDlet {
private Display display;
private Form formA, formB;
public void startApp() {
if (display==null) {
display = Display.getDisplay(this);
formA = new FormA(this);
formB = new FormB(this);
}
display.setCurrent(formA);
}
public Form getFormA() {
return formA;
}
public Form getFormB() {
return formB;
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
FormA.java (this is where you put the content of your Form):
import javax.microedition.lcdui.*;
public class FormA extends Form {
private Command cmdNext;
public FormA(final ExampleMidlet midlet) {
super("Form A");
append("This is form A.");
cmdNext = new Command("Next", Command.SCREEN, 0);
addCommand(cmdNext);
setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
Display.getDisplay(midlet).setCurrent(midlet.getFormB());
}
});
}
}
FormB.java (this is where you put the content of your Form):
import javax.microedition.lcdui.*;
public class FormB extends Form {
private Command cmdBack;
public FormB(final ExampleMidlet midlet) {
super("Form B");
append("This is form B.");
cmdBack = new Command("Back", Command.SCREEN, 0);
addCommand(cmdBack);
setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
Display.getDisplay(midlet).setCurrent(midlet.getFormA());
}
});
}
}
EDIT: I believe I need help getting the selected element in the list I just managed
for it to display a new form but I'm having a lot of trouble finding code that workswith source 3.0.
I've been trying to make a application that allows a user to select a date then add
and remove events based on the selected date. So far I have created the first screen
which is a list of option for the user to choose from. These options are:
Select Date
Add Events
Remove Events
Browse Events
The issues I'm having is I can't get my head around how to display new forms based on the selected Item in the list. I found a small tutorial that allowed me to add a commandlistener which shows the selected item but I'm having trouble figuring out how it gets the item selected in the list and how I could create a new form based on the item selected?
Here's my code so far.
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class mainMidlet extends MIDlet implements CommandListener {
private Display display;
private List list = new List("Please Select a Option", List.IMPLICIT);
private Command select = new Command("Select", Command.SCREEN, 1);
private Form form;
Alert alert;
public mainMidlet() {
display = Display.getDisplay(this);
list.append("Select Date", null);
list.append("Add Events", null);
list.append("Remove Events", null);
list.append("Browse Events", null);
list.addCommand(select);
list.setCommandListener(this);
}
public void startApp() {
display.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == List.SELECT_COMMAND) {
String selection = list.getString(list.getSelectedIndex());
alert = new Alert("Option Selected", selection, null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
}
You can add several forms and switch between them
public void commandAction(Command command, Displayable displayable) {
if (displayable == list) {
if (command == List.SELECT_COMMAND) {
switch (list.getSelectedIndex()) {
case 0: // select date
display.setCurrent(someForm);
break;
case 1: //add events
display.setCurrent(someOtherForm);
break;
}
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
if (displayable == someForm) {
//but it's better practice to make each form a different class implementing CommandListener and it's own commandAction. And leave the display public static in MIDlet class
//...
}
}
I'm trying to make my code neater by using multiple classes for my applications
form options. Currently I keep getting null pointer exceptions when trying to setCurrent.
Here is my main class the error starts in my command listener when I call the other class.
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class CalFrontEnd extends MIDlet implements CommandListener {
private Display display;
private List list = new List("Please Select a Option", List.IMPLICIT);
private List Blist = new List("Please Select a Browsing Option", List.IMPLICIT);
private Command select = new Command("Select", Command.SCREEN, 1);
private Command exit = new Command("Exit", Command.EXIT, 2);
private Command save = new Command("Save,", Command.SCREEN, 2);
private DateField calendar;
Alert alert;
//
//
//
public CalFrontEnd() {
display = Display.getDisplay(this);
list.append("Select Date", null);
list.append("Add Events", null);
list.append("Remove Events", null);
list.append("Browse Events", null);
list.addCommand(select);
list.addCommand(exit);
list.setCommandListener(this);
}
//
//Start Application Method
//
public void startApp() {
display.setCurrent(list);
}
//
//Pause Application Method
//
public void pauseApp() {
}
//
//Destroy Application Method
//
public void destroyApp(boolean unconditional) {
}
//
//Method creates form which contains calendar
//
/*public void selectDate()
{
calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
Form cform = new Form("Calendar");
cform.append(calendar);
cform.addCommand(exit);
display.setCurrent(cform);
}*/
//
//Method creates form which contains adding events
//
public void AddEvents()
{
TextBox aeText = new TextBox("Add Event","", 256, 0);
display.setCurrent(aeText);
}
//
//Method creates form which contains removing events
//
public void RemoveEvents()
{
Form reform = new Form("Remove Event");
reform.append(calendar);
display.setCurrent(reform);
}
//
//Method creates form which contains removing events
//
public void BrowseEvents()
{
Blist.append("Monthly", null);
Blist.append("Daily", null);
Blist.addCommand(select);
Blist.addCommand(exit);
Blist.setCommandListener(this);
display.setCurrent(Blist);
}
//
//but it's better practice to make each form a different class extending CommandListener and it's own commandAction. And leave the display public static in MIDlet class
//...
public void commandAction(Command command, Displayable displayable) {
if (displayable == list) {
if (command == List.SELECT_COMMAND) {
switch (list.getSelectedIndex()) {
case 0: // select date
SelectDate.BuildCalendar(); //Error Here
break;
case 1: //add events
AddEvents();
break;
}
} else if (command == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
}
And here is the class that is being called.
public class SelectDate
{
private static DateField calendar;
private static Form form = new Form("derb");
private static Command select = new Command("Select", Command.SCREEN, 1);
private static Command exit = new Command("Exit", Command.EXIT, 2);
private static Command save = new Command("Save,", Command.SCREEN, 2);
private static Display display;
public static void BuildCalendar()
{
calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
form.append(calendar);
form.addCommand(exit);
display.setCurrent(form);
}
}
The NullPointerException happens because display in SelectDate class is null.
To fix that, you can for example drop it from there and instead, add to method parameters:
// ...
public static void BuildCalendar(Display display) // added parameter
Then, when you invoke above method from CalFrontEnd, pass the instance of display there:
// ...
SelectDate.BuildCalendar(display); //Error will go away
I want to make my phone vibrate when my game ends. I tried using
Display display = Display.getDisplay(midlet);
display.vibrate(2000);
but display.vibrate(2000) returns false and the device does not vibrate.
Can anyone help.
I am trying it on Nokia C7 device. (Symbian^3)
According to Display.vibrate documentation "The return value indicates if the vibrator can be controlled by the application." If you are calling vibrate during destroyApp the VM might be ignoring the vibrate request.
Try calling Display.vibrate before you call MIDlet.notifyDestroyed
Try this code and see if it works.
It worked for me on nokia e63
package ravi.vibrationClass;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Vibrate extends MIDlet implements CommandListener{
Form form;
Display disp;
Command vib,exit;
public void startApp() {
form = new Form("Vibration");
disp = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
vib = new Command("Vibrate", Command.OK, 1);
form.append("Press \"vibrate\" to make the phone vibrate");
form.addCommand(vib);
form.addCommand(exit);
form.setCommandListener(this);
disp.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command c, Displayable arg1) {
if(c == vib){
disp.vibrate(125);
}else if(c == exit){
destroyApp(true);
}
}
}