DELAY 小缓冲写如延迟值。如果为0,则禁用了TCP对于小缓冲区操作的Nagle算法。如果需要启动该算法则需要把该值设置为非0
KEEPLIVE 保持连接的特性。如果该值为0,则禁用了保持连接的特性。如果要启动该特性则要把该值设置为非0
LINGER 关闭一个连接前等待未发送的数据发送完毕所经过的秒数。如果该值为0,则禁用了该属性
RCVBUF 接受缓冲区的大小,单位字节
SNDBUF 发送缓冲区的大小,单位字节
这些细节往往是 重要但是容易乎略的。
最近由于工作需要,需要对背景灯进行操作。对于背景灯设置API说得并不清晰而且缺少例子,网上能找到的资料也不多,这里放上一些最近搜集到的,以作备忘。 CS000957 - Flashing the backlight
S60 3rd Edition, FP1 Tested on devices Nokia E61i
Nokia N95 8GB Category Java ME Subcategory Hardware
Keywords (APIs, classes, methods, functions) : javax.microedition.lcdui.Display, javax.microedition.lcdui.Display.flashBacklight() Overview
This snippet demonstrates how to flash the backlight of the device for a specified duration. In practice, the MIDlet constructs a menu item through which the user can flash the backlight.
SourceFlashing the backlight can be implemented as follows:
// Flash the backlight for 5 seconds Display.getDisplay ( this ) .flashBacklight ( 5000) ;
Here is a complete example:
import javax.microedition.lcdui.Command ; import javax.microedition.lcdui.CommandListener ; import javax.microedition.lcdui.Display ; import javax.microedition.lcdui.Displayable ; import javax.microedition.lcdui.Form ; import javax.microedition.midlet.MIDlet ;
public
class
ExampleMIDlet extends
MIDlet implements
CommandListener {
private
Command flashCommand;
private
Command exitCommand;
private
Form mainForm;
/**
* Constructor. Constructs the object and initializes displayables.
*/
public
ExampleMIDlet(
)
{
mainForm =
new
Form(
"ExampleMIDlet"
)
;
flashCommand =
new
Command(
"Flash"
, Command.SCREEN
, 0)
;
mainForm.addCommand
(
flashCommand)
;
exitCommand =
new
Command(
"Exit"
, Command.EXIT
, 0)
;
mainForm.addCommand
(
exitCommand)
;
mainForm.setCommandListener
(
this
)
;
}
/**
* Called when the MIDlet is started.
*/
public
void
startApp(
)
{
Display.getDisplay
(
this
)
.setCurrent
(
mainForm)
;
}
// Other inherited methods omitted for brevity
// ...
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/
public
void
commandAction(
Command command, Displayable displayable)
{
if
(
command ==
flashCommand)
{
// Flash the backlight for 5 seconds
boolean
flashAllowed =
Display.getDisplay
(
this
)
.flashBacklight
(
5000)
;
if
(
!
flashAllowed)
{
// TODO: Flashing is not allowed. Inform the user.
}
}
else
if
(
command ==
exitCommand)
{
// Exit the MIDlet
destroyApp(
true
)
;
notifyDestroyed(
)
;
}
}
Postconditions
If the user selects the Flash menu item, the backlight is flashed for 5 seconds.
If your application doesn't demand constant key presses, after a while the screen saver on a J2ME phone will start automatically.
To make sure that the display light is turned on, the setLights method should be called before the screen saver is started and this must be done in a loop since the screen saver is not disabled just interrupted.
import
com.nokia.mid.ui.DeviceControl
;
import
javax.microedition.lcdui.*
;
import
javax.microedition.midlet.*
;
public
class
BacklightWorkaround extends
MIDlet {
private
SimpleCanvas canvas;
/**
* Keeps the backlight on by repeatedly setting
*/
class
LightThread extends
Thread
{
public
void
run(
)
{
while
(
true
)
{
DeviceControl.setLights
(
0, 100)
;
try
{
Thread
.sleep
(
5000)
;
}
catch
(
InterruptedException
ex)
{
ex.printStackTrace
(
)
;
}
}
}
}
private
class
SimpleCanvas extends
Canvas
implements
CommandListener{
private
Command exitCmd;
private
MIDlet midlet;
public
SimpleCanvas(
MIDlet midlet)
{
this
.midlet
=
midlet;
exitCmd =
new
Command(
"Exit"
,Command.EXIT
, 1)
;
addCommand(
exitCmd)
;
setCommandListener(
this
)
;
}
public
void
paint(
Graphics
g)
{
g.drawString
(
"Let there be light."
, 0, 0, Graphics
.LEFT
|
Graphics.TOP
)
;
}
public
void
commandAction(
Command command, Displayable displayable)
{
if
(
command ==
exitCmd)
{
midlet.notifyDestroyed
(
)
;
}
}
}
public
void
startApp(
)
{
if
(
canvas ==
null
)
{
canvas =
new
SimpleCanvas(
this
)
;
new
LightThread(
)
.start
(
)
;
}
Display.getDisplay
(
this
)
.setCurrent
(
canvas)
;
}
public
void
pauseApp(
)
{
}
public
void
destroyApp(
boolean
unconditional)
{
}
}
Method
Description
static void flashLights(long duration)
Flashes the lights.
static void setLights(int num, int level)
Turns the lights on and off.
static void startVibra(int freq, long duration)
Vibrates for a given frequency and time.
static void stopVibra()
Stops vibrating.
The two device elements you can control are the lights and vibration. To temporarily flash the lights on and off, use the flashLights method. For example:
import com.nokia.mid.ui.DeviceControl;
…
DeviceControl.flashLights(5000);
This code will cause the lights (such as the LEDs) to flash. If there is no support for this feature, then nothing will happen (duh). The integer value specifies the length of time (in milliseconds) to keep the lights on, although the device might override this value if you specify a number that is too large.
The other method relating to lights, setLights , allows you to control any of the lights on the device individually, such as the backlight or the LEDs …in theory. In reality, Nokia only gives you the ability to control the device's backlight (if it has one). To do this, call the method with the light number (the first integer) set to 0 for the backlight and the level integer set to a number between 0 and 100 (where 0 is off and 100 is the brightest level). For devices that don't support graded backlighting, all values between 1 and 100 just translate to on. Here's an example of setLights in action:
DeviceControl.setLights(0, 100);
NOTE
Tip
A word of warning about playing with the backlight: There is no method to determine the current backlighting level; therefore, you have no way of restoring the lighting to the level the user previously had. Be careful using this function. A user won't be impressed if, while playing in a dark place, you turn the lights out to reward them for completing a level.
If you really want to add this function to your game, consider making it an option the player can enable or disable. This applies to the vibration function as well.
原文连接:http://j2megame.atw.hu/ch06lev1sec122.html
这本书是我在www.iphoneside.com上看到的,Amazon上给出了4星半。于是我把它下载回来看,现在还差最后一张内容和AppendixA没看。总体感觉写的非常好。
这是一本入门书。作为一本入门书,我认为像<C++ Primer>和<Thinking in Java>这样的很不错,可惜有些大,最值得称道的是<Python Tutorial>这样的。把基本的概念告诉你,让我们有个扎实的基础。然后再讲解一些常用库,因为写过程序的都知道,我们不是活在一个真空的世界,无论写什么程序,我们都需要库甚至框架的支持。<Learn Object-C On Mac>非常注意这点。因为我相信现实中,极少有人会在Linux/Windows/Solaris这样的平台上用Object-C,而99.99%的人学习这门语言就是两个目的:(1)iPhone的开发;(2)Mac软件的开发或者移植。所以这本书就是依赖于Cocoa这个框架来讲解Object-C的。而且书中对Cocoa常用的类和特有的技巧(譬如KVC)都有涉猎。
其次,书中的例子规模中等,而且举例恰当。始终不过几个小例子,非常适合这种入门的教材。
最后,英文书看的不算累,语言很幽默。时不时还搞搞笑,让人看得很轻松。
当然了,这本书只是一本入门书,但是我现在感觉看完这本书,我可以很平坦的去学习Mac和iPhone开发了,基础还是很不错的。当然了,我觉得增加一章节介绍线程的可能会更好:)
zhoulei984623@qq.com