当前位置:  编程技术>移动开发
本页文章导读:
    ▪电话接听并查看此人姓名        电话接听并查看该人姓名 <application>  .....  <receiver android:name=".ServiceReceiver">    <intent-filter>      <action android:name="android.intent.action.PHONE_STATE" />    </intent-filter>  <.........
    ▪ M3G课程:进阶篇(一)金字塔        M3G教程:进阶篇(一)金字塔   关于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.........
    ▪ SocketConnection 参数详细引见       SocketConnection 参数详细介绍 请大家看下面的代码: len = is.read(gData); 这句代码会阻塞在这里,用了上面红色标出的代码后,能不能实现,超过5秒没反应后,read就不阻塞,而向下执行代码!.........

[1]电话接听并查看此人姓名
    来源: 互联网  发布时间: 2014-02-18
电话接听并查看该人姓名

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


    
[2] M3G课程:进阶篇(一)金字塔
    来源: 互联网  发布时间: 2014-02-18
M3G教程:进阶篇(一)金字塔

 

关于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倍),然而正常的移动电话屏幕有很多种不同的比例。要得到这个变量的值,你需要做的就是用高除当前屏幕的宽

near、far-:近截面和远截面,定义多近/多远的一个对象依然可以被渲染。那么例如,设置近截面为0.1和远截面为50,意味着所有距离照相机小于0.1单位的对象将不会被渲染。同样所有距离照相机大于50单位的对象也不会被渲染。

 

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

}

 

运行效果如下:


    
[3] SocketConnection 参数详细引见
    来源: 互联网  发布时间: 2014-02-18
SocketConnection 参数详细介绍

请大家看下面的代码: 
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(); 

实现不了吧, 你弄个timer吧。 超时就把所有object 设置为null
 
SocketConnection.DELAY:开启Nagle算法?(0/1) 
SocketConnection.LINGER:服务器悬挂等待时间 
SocketConnection.KEEPALIVE:长连接时间 
SocketConnection.RCVBUF:接收缓冲 
SocketConnection.SNDBUF:发送缓冲 

不过虚拟机上可能支持的不好,或者不保证每次都准确 
建议使用定时器 
超时就把该关流,连接的全关了 
再置null


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3