Hello J2ME gurus,
I am enclosing an example midlet. If you run it in your phone and click 'test' , the midlet will download (http) Google's front page. Just before the download, the phone will , of course, connect to 3G network (duh!) and a white '3G' icon will appear in the top-left corner.
The problem is that even after the download finishes, the icon does NOT disappear no matter how long one waits (I tried 10 minutes). The icon only disappears when one exits the midlet.
Question: why is that? isn't the phone supposed to automatically disconnect after some time of inactivity, say, 1 min?
AFAIK, there are only 2 possibilities:
1) there is something wrong with the code below ( hopefully ) or
2) if a midlet performs network operation, nokia's phones will stay connected no matter how long you run the midlet and only disconnect when you exit it. ( that would suck )
PHP Code:import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class TestDownload extends MIDlet implements CommandListener, Runnable
{
private Display mDisplay;
private TextBox mTextBox;
private Command mTestCommand, mExitCommand;
private static Thread mNetworkThrd = null;
private HttpConnection hc = null;
private DataInputStream din = null;
public TestDownload()
{
mTestCommand = new Command("Test", Command.SCREEN, 1);
mExitCommand = new Command("Exit", Command.EXIT, 1);
mTextBox = new TextBox("Test", "", 32, TextField.ANY);
mTextBox.addCommand(mTestCommand);
mTextBox.addCommand(mExitCommand);
mTextBox.setCommandListener(this);
}
public void startApp()
{
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mTextBox);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{
if (c == mTestCommand)
{
mTextBox.setString("Downloading...");
mNetworkThrd = new Thread(this);
mNetworkThrd.start();
}
if (c == mExitCommand) notifyDestroyed();
}
public void run()
{
try
{
hc= (HttpConnection)Connector.open("http://www.google.com");
int length = (int)hc.getLength();
byte[] data = null;
if( length != -1 )
{
data = new byte[length];
din = new DataInputStream( hc.openInputStream() );
din.readFully(data);
}
mTextBox.setString("data downloaded, length:"+length);
}
catch( IOException ioe ) { mTextBox.setString("IOException" ); }
catch( SecurityException se ) { mTextBox.setString("SecurityException"); }
finally
{
try { if( din!=null ) {din.close(); din=null; } }
catch(IOException ioe) { mTextBox.setString("Exception closing din");}
try { if( hc!=null ) { hc.close(); hc=null; } }
catch(IOException ioe) { mTextBox.setString("Exception closing hc"); }
}
}
}

Reply With Quote



