<application>
.....
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener=new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
public class MyPhoneStateListener extends PhoneStateListener {
private String[] projection = new String[] {
People._ID, People.NAME, People.NUMBER
};
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(!incomingNumber.equals("")){
handleCall(incomingCall);
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
break;
}
}
public void handleCall(String incomingCall){
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
incomingNumber);
contactsCursor = context.getContentResolver().query(contactUri,
projection, null , null, People.NAME + " ASC");
if(contactsCursor.moveToFirst()){
int phoneNameIndex = contactsCursor.getColumnIndex(People.NAME);
String phoneNameStr = contactsCursor.getString(phoneNameIndex);
Log.d("DEBUG",phoneNameStr + " is calling");
}else{
Log.d("DEBUG","Not a contact");
}
}
}
关于World
public class World extend Group
A special Group node that is a top-level container for scene graphs.
A scene graph is constructed from a hierarchy of nodes. In a complete scene graph, all nodes are ultimately connected to each other via a common root, which is a World node. An example of a complete scene graph is shown in the figure below.
Note that a scene graph need not be complete in order to be rendered; individual nodes and branches can be rendered using a separate method in Graphics3D. However, the semantics of rendering an incomplete scene graph are slightly different compared to rendering a World; see Graphics3D for more information
Despite that it is called a graph, the scene graph is actually a tree structure. This implies that a node can belong to at most one group at a time, and cycles are prohibited. However, component objects, such as VertexArrays, may be referenced by an arbitrary number of nodes and components. The basic rules for building valid scene graphs are summarized below.
Even though World is a scene graph node, its special role as the singular root node has two noteworthy consequences. Firstly, a World can not be a child of any Node. Secondly, the node transformation is ignored for World objects when rendering. In all other respects (get, set, animate), the transformation behaves just like any other node transformation. Note also that there is no conceptual "Universe" coordinate system above the World, contrary to some other scene graph APIs.
The method render(World) in Graphics3D renders a World as observed by the currently active camera of that world. If the active camera is null, or the camera is not in the world, an exception is thrown. The world can still be rendered with the render(Node,Transform) method by treating the World as a Group. In that case, however, the application must explicitly clear the background and set up the camera and lights prior to rendering.
关于Camera
setPerspective (float fovy, float aspectRatio, float near, float far)
其中
fovy:代表在y轴上的视野,它的正常值时在(0,90)度之间的范围内。
注意: 因为摄像机是面向负Z轴 (0,0,-1), 所以它这个范围表示在竖直方向可视范围在这个读数之间.( h = tan(fovy/2))
aspectRatio:屏幕高宽比,这是一个相当简单的参数,它是一个分数,告诉引擎当前屏幕的宽和高的关系。大多数计算机屏幕的比例是4:3(也就是高是宽的0.75倍),然而正常的移动电话屏幕有很多种不同的比例。要得到这个变量的值,你需要做的就是用高除当前屏幕的宽
import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.m3g.Appearance; import javax.microedition.m3g.Camera; import javax.microedition.m3g.Graphics3D; import javax.microedition.m3g.IndexBuffer; import javax.microedition.m3g.Mesh; import javax.microedition.m3g.PolygonMode; import javax.microedition.m3g.TriangleStripArray; import javax.microedition.m3g.VertexArray; import javax.microedition.m3g.VertexBuffer; import javax.microedition.m3g.World; public class M3GCanvas extends GameCanvas implements Runnable { public static final int FPS = 20; //每秒绘制的帧数 private Graphics3D g3d; private World world; private boolean runnable=true; private Thread thread; private Camera camera; // the camera in the scene private Mesh pyramidMesh; // the pyramid in the scene protected M3GCanvas() { super(false); setFullScreenMode(true); g3d = Graphics3D.getInstance(); world = new World(); camera = new Camera(); world.addChild(camera); // add the camera to the world. float w = getWidth(); float h = getHeight(); // Constructs a perspective projection matrix and sets that as the current projection matrix. //setPerspective (float fovy, float aspectRatio, float near, float far) camera.setPerspective(60.0f, w / h, 0.1f, 50f); pyramidMesh = createpyramid(); // create our pyramid. //将对象沿Z轴移动-4个单位 pyramidMesh.setTranslation(0.0f, 0.0f, -4.0f); world.addChild(pyramidMesh); // add the pyramid to the world //Sets the Camera to use when rendering this World. world.setActiveCamera(camera); } public void run() { Graphics g = getGraphics(); while (runnable) { long startTime = System.currentTimeMillis(); // rotate the pyramid 3 degree around the Y-axis. //postRotate(float angle,float ax,float ay,float az) pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f); try { g3d.bindTarget(g); g3d.render(world); } finally { g3d.releaseTarget(); } flushGraphics(); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; if(costTime<1000/FPS) { try{ Thread.sleep(1000/FPS-costTime); } catch(Exception e){ e.printStackTrace(); } } } System.out.println("Canvas stopped"); } public void start() { thread=new Thread(this); thread.start(); } public void stop() { this.runnable=false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 创建金字塔 * @return */ private Mesh createpyramid(){ /** * 组成金字塔的五个顶点 */ short[] points = new short[]{ -1, -1, 1, //左前 1, -1, 1, //右前 1, -1, -1, //右后 -1, -1, -1, //左后 0, 1, 0 //顶 }; /** * 点索引,使用逆时针指定五个面,底面是一个四边形,分解成两个三角形 */ int[] indices = new int[]{ 0, 1, 4, //前 1, 2, 4, //右 2, 3, 4, //背 3, 0, 4, //左 2, 1, 0, //底1 2, 0, 3 //底2 }; /** * 各个顶点颜色 */ byte[] colors = new byte[]{ -1, 0, 0, //红色 0, -1, 0, //绿色 0, 0, -1, //蓝色 -1, 0, -1, //紫色 -1, -1, 0 //黄色 }; // The length of each sequence in the indices array. int[] length = new int[]{3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles VertexArray vertexArray, colorArray; IndexBuffer indexBuffer; //准备顶点数组用于创建顶点缓存,short型为2个字节 //VertexArray(int numVertices, int numComponents, int componentSize) vertexArray = new VertexArray(points.length/3, 3, 2); //复制数据 //set(int firstVertex, int numVertices, short[] values) vertexArray.set(0, points.length/3, points); //同样操作颜色数据,byte为1个字节 colorArray = new VertexArray(colors.length/3, 3, 1); colorArray.set(0, colors.length / 3, colors); indexBuffer = new TriangleStripArray(indices, length); // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals, // and texture coordinates for a set of vertices VertexBuffer vertexBuffer = new VertexBuffer(); vertexBuffer.setPositions(vertexArray, 1.0f, null); vertexBuffer.setColors(colorArray); // Create the 3D object defined as a polygonal surface //Constructs a new Mesh consisting of only one submesh. //Mesh(VertexBuffer vertices, IndexBuffer submesh, Appearance appearance) Mesh mesh = new Mesh(vertexBuffer, indexBuffer, null); //Appearance用于定义Mesh或者Sprite3D的渲染属性,由基于组件对象组成 //每个组件对象又由一系列相互关联的属性组成 Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes polygonMode.setPerspectiveCorrectionEnable(true); polygonMode.setWinding(PolygonMode.WINDING_CCW);//设置逆时针顺序为正面 polygonMode.setCulling(PolygonMode.CULL_BACK); // 不画背面,如果为CULL_NONE只两面都画 polygonMode.setShading(PolygonMode.SHADE_SMOOTH); //设置投影模式为光滑模式,也可以设置为平面模式SHADE_FLAT appearance.setPolygonMode(polygonMode); //setAppearance(int index, Appearance appearance) Sets the Appearance for the specified submesh. mesh.setAppearance(0, appearance); // Set the appearance to the 3D object return mesh; } }
运行效果如下:
len = is.read(gData);
这句代码会阻塞在这里,用了上面红色标出的代码后,能不能实现,超过5秒没反应后,read就不阻塞,而向下执行代码!
try
{
int len = 0;
sc = (SocketConnection) Connector.open("socket://100.42.25.3:885");
is = sc.openInputStream();
os = sc.openOutputStream();
sc.setSocketOption(SocketConnection.LINGER, 5);
pmm.gDataBuf = null;
sender = new Sender(os);
sender.send(pmm.gStringReq);
// Loop forever, receiving data
gData = new byte[pmm.BUF_LENGTH];
currentIndex = 0;
gBuf = new byte[50*1024];
while (!pmm.bStopConnect)
{
len = is.read(gData);
System.arraycopy(gData,0,gBuf,currentIndex,len);
currentIndex += len;
if(currentIndex > 300){
if(newVerifyXml()){
if((currentIndex%8) != 0){
int left = 8 - currentIndex%8;
byte[] byteTmp = new byte[left];
is.read(byteTmp);
}
opHandle();currentIndex=0;
}
}
}
stop();
} catch (ConnectionNotFoundException cnfe) {
Alert a = new Alert("错误", "无法连接服务器", null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a) ;
} catch (IOException ioe) {
if (!stop) {
ioe.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
SocketConnection.LINGER:服务器悬挂等待时间
SocketConnection.KEEPALIVE:长连接时间
SocketConnection.RCVBUF:接收缓冲
SocketConnection.SNDBUF:发送缓冲
不过虚拟机上可能支持的不好,或者不保证每次都准确
建议使用定时器
超时就把该关流,连接的全关了
再置null