編輯:初級開發
public class CompassActivity extends Activity implements Renderer, SensorEventListener {
private GLSurfaceView mGLSurfaceView; //GL VIEw
private SensorManager mSensorManager;
private float[] mGData = new float[3];
private float[] mMData = new float[3];
private float[] mR = new float[16];
private float[] mI = new float[16];
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private float[] mOrIEntation = new float[3];
private int mCount;
public CompassActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mGLSurfaceView = new GLSurfaceVIEw(this);
mGLSurfaceVIEw.setRenderer(this);
setContentView(mGLSurfaceVIEw);
}
@Override
protected void onResume() { //Activity切換到前端時觸發
super.onResume();
mGLSurfaceVIEw.onResume();
Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //加速感應器
Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //磁力感應器
mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() { //Activity切換到後台時觸發
super.onPause();
mGLSurfaceVIEw.onPause();
mSensorManager.unregisterListener(this); //主要是為了省電
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -2);
gl.glMultMatrixf(mR, 0);
gl.glVertexPointer(3, GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL_FLOAT, 0, mColorBuffer);
gl.glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, mIndexBuffer);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glVIEwport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glClearColor(1,1,1,1);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnableClIEntState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClIEntState(GL10.GL_COLOR_ARRAY);
float vertices[] = {
0,0,0,
1,0,0,
0,1,0,
0,0,1
};
float colors[] = {
0,0,0,0,
1,0,0,1,
0,1,0,1,
0,0,1,1
};
byte indices[] = { 0, 1, 0, 2, 0, 3 };
ByteBuffer vbb;
vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
vbb = ByteBuffer.allocateDirect(colors.length*4);
vbb.order(ByteOrder.nativeOrder());
mColorBuffer = vbb.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) { //實現SensorEventListener 接口
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = mGData;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = mMData;
} else {
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
SensorManager.getOrientation(mR, mOrIEntation);
float incl = SensorManager.getInclination(mI);
if (mCount++ > 50) {
final float rad2deg = (float)(180.0f/Math.PI);
mCount = 0;
Log.d("Compass", "yaw: " + (int)(mOrIEntation[0]*rad2deg) +
" pitch: " + (int)(mOrIEntation[1]*rad2deg) +
" roll: " + (int)(mOrIEntation[2]*rad2deg) +
" incl: " + (int)(incl*rad2deg)
);
}
}
}
當我們在應用程序Launcher的桌面空白處長按觸摸時,會出現一個對話框,提示選擇要添加的桌面組件,如下圖所示選擇快捷方式後,會彈出一個對話框,顯示出了可添加快捷方式的
本人嘗試,裡面提供得軟件在索愛x8上失敗了 提示說不能網絡共享 。看來自己編譯x8迫在眉睫啊 (有幸下載到了x8得源代碼。。。)--------------------
下面展示一段在android1.5上讀取手機通訊錄的代碼1 //鏈接通訊錄數據庫 2 ContentResolver content = getContentResol
ps:對於軟件開發人員來說,保護代碼安全也是比較重要的因素之一,不過目前來說Google Android平台選擇了Java Dalvik VM的方式使其程序很容易破解和