锁定手机的数据

时间:2014-11-02 23:38:27

标签: android service

我写了这样的东西,我可以运行它,但它不起作用。有人帮忙吗?请耐心等待:D 我需要在手机锁定时从加速度计保存数据并将其放在口袋里。 感谢

MainActivity:

package emzet.acc2txt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

private Button btnStart, btnStop;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnStart = (Button) findViewById(R.id.btnStart);
    btnStop = (Button) findViewById(R.id.btnStop);
    btnStart.setEnabled(true);
    btnStop.setEnabled(true);
}

public void onStartClick(View view) {
    startService(new Intent(this,accService.class));
    btnStop.setEnabled(true);
    btnStart.setEnabled(false);
}

public void onStopClick(View view) {
    stopService(new Intent(this, accService.class));
    btnStart.setEnabled(true);
    btnStop.setEnabled(false);
}



}

为MyService:

package emzet.acc2txt;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Environment;
import android.os.IBinder;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;


public class accService extends Service implements SensorEventListener{

String root = Environment.getExternalStorageDirectory().toString();
protected static SensorManager sensorManager;
protected static Sensor accelerometer;
private FileWriter writer;


@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate(){
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

@Override
public void onDestroy(){
    if(writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    sensorManager.unregisterListener(this);
    super.onDestroy();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        writer = new FileWriter(root + "/pokus.txt",true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public void onSensorChanged(SensorEvent event) {

    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss:SSS");
    String s = time.format(new java.util.Date());
    try {
        writer.write(s+"\t"+x+"\t"+y+"\t"+z+"\n");
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

有任何帮助吗? 我当然也编辑了清单...

1 个答案:

答案 0 :(得分:0)

您忘了在服务中的OnCreate中设置sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);