android 中使用service主要是作一些费时的,影响用户体验的操作。但是service实际上也是运行在主线程中,你如果不再serivce中新作线程来进行以上的费时操作的话,一样会影响用户的操作。
这个地方一直很疑惑?既然都是作thread来作费时的操作为什么还要作service呢。
参阅了guide上的这篇文章《Processes and Threads》,明白了android 对老进程的销毁原则:service的重要度要大于activity,因此对于一些运行在后台的线程,应该声明在service中,以避免被系统销毁。
以下是原文中的一段解释:
Because a process running a service is ranked higher than a process with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply create a worker thread—particularly if the operation will likely outlast the activity. For example, an activity that's uploading a picture to a web site should start a service to perform the upload so that the upload can continue in the background even if the user leaves the activity. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. This is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.
mainActivity:public class OpenGLDemoActivity extends Activity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 需要借助GLSurfaceView来渲染Render GLSurfaceView mGlSurfaceView = new GLSurfaceView(this); mGlSurfaceView.setRenderer(new TriggleRender()); // mGlSurfaceView.setRenderMode() setContentView(mGlSurfaceView); } }
Render的实现类:
public class TriggleRender implements Renderer { @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub ByteBuffer vbb= ByteBuffer.allocateDirect(vertexArray.length*4); vbb.order(ByteOrder.nativeOrder()); FloatBuffer vertex = vbb.asFloatBuffer(); vertex.put(vertexArray); vertex.position(0); gl.glLoadIdentity(); gl.glTranslatef(0, 0, -4); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex); index++; index%=10; switch(index){ case 0: case 1: case 2: gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6); break; case 3: case 4: case 5: gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6); break; case 6: case 7: case 8: case 9: gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6); break; } gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub //这句没有,不能显示效果 gl.glViewport(0, 0, width, height); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub } float vertexArray[] = { -0.8f, -0.4f * 1.732f, 0.0f, 0.0f, -0.4f * 1.732f, 0.0f, -0.4f, 0.4f * 1.732f, 0.0f, 0.0f, -0.0f * 1.732f, 0.0f, 0.8f, -0.0f * 1.732f, 0.0f, 0.4f, 0.4f * 1.732f, 0.0f, }; int index=0; }
此类为画线段:
public class OpenGLRender implements Renderer { @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub ByteBuffer vbb= ByteBuffer.allocateDirect(vertexArray.length*4); vbb.order(ByteOrder.nativeOrder()); FloatBuffer vertex = vbb.asFloatBuffer(); vertex.put(vertexArray); vertex.position(0); gl.glLoadIdentity(); gl.glTranslatef(0, 0, -4); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex); index++; index%=10; switch(index){ case 0: case 1: case 2: gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINES, 0, 4); break; case 3: case 4: case 5: gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 4); break; case 6: case 7: case 8: case 9: gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4); break; } gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } @Override public void onSurfaceChanged(GL10 arg0, int width, int height) { // TODO Auto-generated method stub arg0.glViewport(0, 0, width, height); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig arg1) { // TODO Auto-generated method stub // gl.glClearColor(0.9f, 0.2f, 0.2f, 1.0f); // gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } float vertexArray[] = { -0.8f, -0.4f * 1.732f, 0.0f, -0.4f, 0.4f * 1.732f, 0.0f, 0.0f, -0.4f * 1.732f, 0.0f, 0.4f, 0.4f * 1.732f, 0.0f, }; int index=0;
参考自:eoeAndroid上的文章
http://v.youku.com/v_playlist/f4889604o1p1.html