Android模擬器學framework和driver之傳感器篇4(Android APP)

上面已經介紹瞭,android temperature sensor的移植過程,代碼頁已經貼給大傢瞭,現在我們寫一個APK來進行測試,代碼很簡單,界面也很簡單很醜陋,哈哈,大傢不要介意,這個aok隻是用作測試,這裡我就不多做介紹瞭,直接貼代碼。
 
 
 
package com.android.jay.sensor1; 
 
import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public  class MySensor1Activity extends Activity implements SensorEventListener { 
    /** Called when the activity is first created. */ 
    private TextView tView; 
    SensorManager sensorManager = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);  
        tView=(TextView)findViewById(R.id.tv); 
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE),sensorManager.SENSOR_DELAY_GAME); 
    } 
     
    public void onSensorChanged(SensorEvent event) { 
        if(event.sensor.getType()==Sensor.TYPE_TEMPERATURE) 
            tView.setText("value: "+Float.toString(event.values[0])); 
         
    } 
 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
        // TODO Auto-generated method stub  
         
    } 

package com.android.jay.sensor1;
 
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
 
public  class MySensor1Activity extends Activity implements SensorEventListener {
    /** Called when the activity is first created. */
       private TextView tView;
       SensorManager sensorManager = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tView=(TextView)findViewById(R.id.tv);
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE),sensorManager.SENSOR_DELAY_GAME);
    }
   
    public void onSensorChanged(SensorEvent event) {
           if(event.sensor.getType()==Sensor.TYPE_TEMPERATURE)
                  tView.setText("value: "+Float.toString(event.values[0]));
             
       }
 
       public void onAccuracyChanged(Sensor sensor, int accuracy) {
              // TODO Auto-generated method stub
             
       }
}
最後用adb install xxx.apk安裝程序,然後再模擬器上運行,結果如下:
 
 
 
 

 

 
 
OK,看到value在不斷的變化瞭吧,完工,下面一節我們會介紹framework層如何處理的。。。

摘自 zhangjie201412的專欄

發佈留言