📄 exportobexprogress.java
字号:
// J2ME GPS Track
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/gpstrack
package org.qcontinuum.gpstrack;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import de.avetana.javax.obex.*;
public class ExportObexProgress extends Form implements CommandListener, Runnable {
private Displayable mParent;
private String mTrackName;
private int mFormat;
private Gauge mProgressGauge;
private Command mCancelCommand;
public ExportObexProgress(Displayable parent, String trackName, int format) {
super("Bluetooth");
mParent = parent;
mTrackName = trackName;
mFormat = format;
append(new StringItem("Sending...", "\n"));
append(mProgressGauge = new Gauge(null, false, 10, 0));
addCommand(mCancelCommand = new Command("Cancel", Command.CANCEL, 0));
setCommandListener(this);
new Thread(this).start();
}
public void commandAction(Command c, Displayable d) {
if (c == mCancelCommand) {
GpsTrack.display(mParent);
}
}
public void run() {
Thread.currentThread().yield();
if (GpsTrack.obexCapable())
sendTrackNative();
else
sendTrackAvetana();
}
private void sendTrackNative() {
DataOutputStream outputStream = null;
GpsTrack.getGps().close();
String url = GpsTrack.getPreferences().getObexUrl();
String filename = mTrackName.replace(' ', '_').replace(':', '_') + Track.getFormatExtension(mFormat);
ObexUsage obexUsage = null;
try {
// optional package wrapper indirect call
// technique described in Chapter 8 of JSR 248
obexUsage = (ObexUsage)Class.forName("org.qcontinuum.gpstrack.ObexManager").newInstance();
outputStream = obexUsage.open(url, filename);
Track.write(mFormat, outputStream, mTrackName, mProgressGauge);
GpsTrack.display(mParent);
} catch (Exception ex) {
GpsTrack.display(ex, mParent);
} finally {
try {
if (outputStream != null)
outputStream.close();
if (obexUsage != null)
obexUsage.close();
} catch (IOException ex) { }
}
}
private void sendTrackAvetana() {
ClientSession clientSession = null;
Operation operation = null;
DataOutputStream outputStream = null;
GpsTrack.getGps().close();
String url = GpsTrack.getPreferences().getObexUrl();
try {
try {
clientSession = (ClientSession)Connector.open(url);
} catch (ConnectionNotFoundException ex) {
clientSession = (ClientSession)de.avetana.obexsolo.OBEXConnector.open(url);
}
HeaderSet headerSet = clientSession.connect(clientSession.createHeaderSet());
headerSet.setHeader(HeaderSet.NAME, mTrackName + Track.getFormatExtension(mFormat));
headerSet.setHeader(HeaderSet.TYPE, "text");
operation = clientSession.put(headerSet);
outputStream = operation.openDataOutputStream();
Track.write(mFormat, outputStream, mTrackName, mProgressGauge);
GpsTrack.display(mParent);
} catch (Exception ex) {
GpsTrack.display(ex, mParent);
} finally {
try {
if (outputStream != null)
outputStream.close();
if (operation != null)
operation.close();
if (clientSession != null) {
clientSession.disconnect(null);
clientSession.close();
}
} catch (IOException ex) { }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -