nokia device vibration in a game using j2me - java-me

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);
}
}
}

Related

Getting phone hearing volume with Java ME

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){}
}
}

How to detect key pressed event in LWUIT form?

I have written simple j2me program with LWUIT package.I have added one Form in my MIDLET class file. Suppose,user press a key then I want to show another Form.But I couldn't be able to capture key event in my LWUIT Form.
This is my code snippt
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class MultipleForm extends MIDlet implements ActionListener{
private Form mFirstForm, mSecondForm;
public void startApp()
{
if (mFirstForm == null)
{
Display.init(this);
mFirstForm = new Form("First Form");
Button button = new Button("Switch");
button.addActionListener(this);
mFirstForm.addComponent(button);
mSecondForm = new Form("Second Form");
Button button2 = new Button("Switch");
button2.addActionListener(this);
mSecondForm.addComponent(button2);
mFirstForm.show();
}
}
protected void keyPressed(int key)
{
System.out.println("Key Pressed");
if(key==52)
{
Form current = Display.getInstance().getCurrent();
if (current == mFirstForm)
{
mSecondForm.show();
}
else if(current==mSecondForm)
{
mFirstForm.show();
}
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
To capture the event key in a LWUIT Form you need to use Form.addGameKeyListener(here the key, here actionListener)
The keys are mapped using Canvas like Canvas.FIRE for example.
Try to do that.

How to show parent midlet from another java program in j2me

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.

j2me Threading with video component

i have tried to implement an java app which have following structure.
my problems are
when i invoke quotes thread from videoplayer thread the video still plays on top of the quotes form.
when i change video url with action event it just appends new player with current one.
ex. video2 is append along with currently running video1 when i press video 2 button
.
class VideoPlayer implements Runnable,ActionListener{
private videoappMidlet MIDlet;
VideoComponent vc;
Button Videos,quotes,video1,video2,video3;
Form videoplayer;
Thread thread;
public VideoPlayer(videoappMidlet MIDlet){
this.MIDlet = MIDlet;
}
public void run(){
try{
videoplayer=new Form();
video1=new Button("video1");
.......
vc = VideoComponent.createVideoPeer("http://localhost/video1.mpg");
vc.start();
quotes.addActionListener((ActionListener) this);
........
videoplayer.addComponent(vc);
........
videoplayer.show();
}catch(Exception error){
System.err.println(error.toString());
}
}
public void start(){
thread = new Thread(this);
try{ thread.start();}
catch(Exception error){}
}
public void actionPerformed(ActionEvent ae) {
if((ae.getSource()==Quotes))
{
Quotes tp = new Quotes(this.MIDlet);
tp.start();
}
if(ae.getSource()==video1)
{
try {
vc = VideoComponent.createVideoPeer("http://localhost/video1.mpg");
vc.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
....
}
}
class Quotes implements Runnable,ActionListener {
private videoappMidlet MIDlet;
Button Videos,quotes;
Form quote;
Thread thread;
public Quotes(videoappMidlet MIDlet){
this.MIDlet = MIDlet;
}
public void run(){
try{
quote=new Form();
Videos=new Button("Videos");
........
quote.addComponent(Videos);
........
Videos.addActionListener(this);
........
quote.show();
}catch(Exception error){
System.err.println(error.toString());
}
}
public void start(){
thread = new Thread(this);
try{ thread.start();}
catch(Exception error){}
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==Videos)
{
VideoPlayer vp = new VideoPlayer(this.MIDlet);
vp.start();
}
}
}
public class videoappMidlet extends MIDlet implements ActionListener{
Button play,quote;
Form home;
public void startApp() {
Display.init(this);
home=new Form();
play.addActionListener(this);
quote.addActionListener(this);
home.show();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==play)
{
VideoPlayer vp = new VideoPlayer(this);
vp.start();
}
if(ae.getSource()==quote)
{
Quotes tp = new Quotes(this);
tp.start();
}
}
}
Generally video in JavaME makes no guarantee to the layer in which it is playing. LWUIT tries to seamlessly pause video player for things like a dialog on top of the UI.
As a side note LWUIT is not thread safe and you must not use a separate thread to access the UI since it will break on different platforms.

J2ME java.io.IOException error

I have the following code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class FileConnection extends MIDlet implements CommandListener, Runnable {
private Command exit, start;
private Display display;
private Form form;
public FileConnection ()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.EXIT, 1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException
{
display.setCurrent(form);
}
public void run(){
try{
javax.microedition.io.file.FileConnection filecon =
(javax.microedition.io.file.FileConnection)
Connector.open("file:///root1/photos/fisier.txt", Connector.WRITE);
OutputStream out = filecon.openOutputStream();
PrintStream output = new PrintStream( out );
output.println( "This is a test." );
out.close();
filecon.close();
Alert alert = new Alert("Completed", "Data Written", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( ConnectionNotFoundException error )
{
Alert alert = new Alert(
"Error", "Cannot access file.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( IOException error )
{
Alert alert = new Alert("Error", error.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (command == start)
{
new Thread(this).start();
}
}
}
As you can see, I'm trying to write something in a text file, from an emulator. I run that code in a separate thread, to avoid that warning at the runtime. I have in C:\Program Files\WTK2.5.2_01\j2mewtk_template\appdb\DefaultColorPhone\filesystem\root1\photos a file named fisier.txt. When I try to run this code, and press 'Start', I hit 'Yes' at the question 'J2ME... Midlet Suite wants to write the local file system. It's OK to update your files? YES/NO'. And I got on the screen java.io.IOException:, and nothing more!..
What's wrong? Why I got that error? I did not find anywhere a working code, of how to write to a local .txt file.
Don't know what's wrong in my code?
Could be anything from getting the path wrong (and the file not existing), to you not having write access to it, to it being open elsewhere.
Have you tried calling some of the methods the FileConnection class offers, such as canWrite(), exists(), and isOpen(), to see if some of these common problems apply in your case?

Resources