2013-06-18 200 views
1

首先抱歉轉貼問題,因爲我之前沒有發佈我的代碼,現在只寫了一些行,我沒有解釋。所以再次用代碼發佈問題。 我已經編寫了一個代碼,用於記錄加速度數據並將數據保存在csv文件中。我需要做的是我必須在後臺運行加速器作爲服務。所以,我完成了編寫代碼 1.運行加速度計, 2.顯示文本框中的數據, 3.將數據寫入csv文件。 剩下的事情就是讓它成爲一項服務。我米給下面的代碼(Howfar我做了)Android加速度計服務

 @SuppressWarnings("deprecation") 
public class AccelerometerVaue extends Activity implements SensorListener { 

    SensorManager sm = null; 

    TextView xacc= null; 
    TextView yacc = null; 
    TextView zacc = null; 
    TextView xorient = null; 
    TextView yorient = null; 
    TextView zorient = null; 
    TextView text = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     sm = (SensorManager) getSystemService(SENSOR_SERVICE); 
     setContentView(R.layout.activity_main); 


     xacc = (TextView) findViewById(R.id.xvalue); 
     yacc = (TextView) findViewById(R.id.yvalue); 
     zacc = (TextView) findViewById(R.id.zvalue); 
     xorient = (TextView) findViewById(R.id.xvalues); 
     yorient = (TextView) findViewById(R.id.yvalues); 
     zorient = (TextView) findViewById(R.id.zvalues); 


    } 



    @SuppressLint("SdCardPath") 
    public void onSensorChanged(int sensor, float[] values) { 
     synchronized (this) { 

      if (sensor == SensorManager.SENSOR_ORIENTATION) { 
       xorient.setText("Orientation X: " + values[0]); 
       yorient.setText("Orientation Y: " + values[1]); 
       zorient.setText("Orientation Z: " + values[2]); 
      } 
      if (sensor == SensorManager.SENSOR_ACCELEROMETER) { 
//    Time now = new Time(); 
//    now.setToNow(); 
       SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
       String currentDateandTime = sdf.format(new Date()); 
       Log.d("Time",currentDateandTime); 


       xacc.setText("Accel X: " + values[0]); 
       yacc.setText("Accel Y: " + values[1]); 
       zacc.setText("Accel Z: " + values[2]); 
       String res=String.valueOf(currentDateandTime+"#"+values[0])+"#"+String.valueOf(values[1])+"#"+String.valueOf(values[2]); 

       Log.d("test", res); 


       CSVWriter writer = null; 
       try 
       { 
        //Log.d("check","pasla"); 
        //Environment.getExternalStorageDirectory().getPath(); 
       writer = new CSVWriter(new FileWriter("/sdcard/AccelData.csv",true), ','); 
       String[] entries = res.split("#"); // array of your values 

       writer.writeNext(entries); 
       //FileWriter 
       writer.close(); 
       } 
       catch (IOException e) 
       { 
       //error 
       } 
      }    
     } 
    } 

    public void onAccuracyChanged(int sensor, int accuracy) { 
     String tag = null; 
     Log.e(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy); 

    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 
     sm.registerListener(this, 
       SensorManager.SENSOR_ORIENTATION | 
       SensorManager.SENSOR_ACCELEROMETER, 
       SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onStop() { 
     sm.unregisterListener(this); 
     super.onStop(); 
    }  


} 

所以,現在我需要運行一個後臺服務,將保持在後臺以加速度計數據,並保持保存成csv文件。我怎樣才能做到這一點?我想再問一次,是否可以在一段時間後採取acc數據?假設我在這裏連續記錄所有的數據,這是巨大的,並且電池消耗將是巨大的。所以,我需要以這樣的方式實現服務,以便它能夠在後臺運行15分鐘,並在每分鐘開始時將數據記錄10秒鐘。

回答

0

我剛剛回復了similar的問題。

您可以使用Android的調度機制(調用AlarmManager)來調度您的服務。

雖然這種方法存在問題,但是:當系統處於電池節能睡眠狀態時,服務必須獲得喚醒鎖以便正確執行,然後在完成時釋放系統的喚醒鎖回到睡眠狀態。實現這個喚醒鎖獲取/釋放機制不是一項簡單的任務。

我建議使用Commonsware的WakefulIntentService以避免爲您的服務編寫自己的喚醒鎖獲取/釋放機制。它非常易於使用。你基本上會聲明一個擴展WakefulIntentService類的類並覆蓋它的doWakefulWork()方法。在該方法中,您將輪詢加速度計數據並將其寫入文件。我只是讓你在屏幕上顯示數據的一部分,我不知道該怎麼做(我想你將不得不執行綁定到服務,雖然)。 < :)

+0

AlarmManager用於調度,但首先我想爲我的代碼啓動服務實現。假設我開始無限期的服務,我會稍後手動停止服務。怎麼做?沒有找到正確的方法來做到這一點 – MetaRs

+0

我相信如果您有一個負責調度/取消「AlarmManager」中的服務的Activity,並且保留對用於在應用程序中執行調度的同一個PendingIntent的引用例如作爲全局靜態變量)。當您打算取消預先安排的服務時,您需要提供對「AlarmManager」的引用。 – Piovezan