唤醒锁不能正常工作

时间:2014-11-05 14:44:53

标签: android accelerometer gyroscope

我写了一个程序,其中我有两个服务,一个我从加速度计收集数据,第二个我从陀螺仪收集数据。 我在两者中都使用了wake_lock。 问题是它只在我从android工作室运行时工作,然后当我第二次从手机运行它时它没有工作并且当我锁定手机时停止收集数据。 你能帮我解决问题吗? THx的

package emzet.acc2txt;

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

public class MainActivity extends Activity {

private static final String LOG_TAG = "aktivita" ;
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);
    Log.d( LOG_TAG, "ON CREATE" );

}

public void onStartClick(View view) {
    Intent serviceIntentAcc = new Intent(this, accService.class );
    startService(serviceIntentAcc);
    Intent serviceIntentGyro = new Intent(this, gyroService.class );
    startService(serviceIntentGyro);
    btnStop.setEnabled(true);
    btnStart.setEnabled(false);
    Log.d(LOG_TAG, "ON START CLICK");

}

public void onStopClick(View view) {
    stopService(new Intent(this, accService.class));
    stopService(new Intent(this, gyroService.class));
    btnStart.setEnabled(true);
    btnStop.setEnabled(false);
    Log.d( LOG_TAG, "ON STOP CLICK" );
}



}

加速度计服务:

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 android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;

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

/**
 * Created by Miroslav on 2.11.2014.
 * ALL Rights reserved to eMZet
 */
public class accService extends Service implements SensorEventListener{

private static final String LOG_TAG = "aktivita" ;
String root = Environment.getExternalStorageDirectory().toString();
protected static SensorManager sensorManager;
// protected static SensorManager sensorManager2;
protected static Sensor accelerometer;
// protected static Sensor gyroscope;
private FileWriter writer;
private PowerManager.WakeLock wakeLock;

@Override
public IBinder onBind(Intent intent) {
    Log.d(LOG_TAG, "SERVICE - ON BIND");
    return null;
}

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

    Toast.makeText(this, "Zapisujem . . . ", Toast.LENGTH_LONG).show();
    // register accelerometer sensor
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    // register gyroscope sensor
    //sensorManager2.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);

    Log.d(LOG_TAG, "SERVICE - ON CREATE");

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "funguj");
    wakeLock.acquire();
}

@Override
public void onDestroy(){
    super.onDestroy();
    if(writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    sensorManager.unregisterListener(this);
    Log.d(LOG_TAG, "SERVICE - ON DESTROY");
    wakeLock.release();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    try {
        writer = new FileWriter(root + "/data_acc.txt",true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d(LOG_TAG, "SERVICE - ON START COMMAND");
    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+"|"+x+"|"+y+"|"+z+"\n");
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

陀螺仪服务:

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 android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;

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

/**
 * Created by Miroslav on 2.11.2014.
 * ALL Rights reserved to eMZet
 */
public class gyroService extends Service implements SensorEventListener{

private static final String LOG_TAG = "aktivita" ;
String root = Environment.getExternalStorageDirectory().toString();
protected static SensorManager sensorManager;
protected static Sensor gyroscope;
private FileWriter writer;
private PowerManager.WakeLock wakeLockGyro;

@Override
public IBinder onBind(Intent intent) {
    Log.d(LOG_TAG, "SERVICE - ON BIND");
    return null;
}

@Override
public void onCreate(){
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    // gyroscope
    gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

    Toast.makeText(this, "Zapisujem . . . ", Toast.LENGTH_LONG).show();
    // register gyroscope sensor
    sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);

    Log.d(LOG_TAG, "SERVICE - ON CREATE");

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLockGyro = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "funguj");
    wakeLockGyro.acquire();
}

@Override
public void onDestroy(){
    super.onDestroy();
    if(writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    sensorManager.unregisterListener(this);
    Log.d(LOG_TAG, "SERVICE - ON DESTROY");
    wakeLockGyro.release();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    try {
        writer = new FileWriter(root + "/data_gyro.txt",true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d(LOG_TAG, "SERVICE - ON START COMMAND");
    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+"|"+x+"|"+y+"|"+z+"\n");
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

1 个答案:

答案 0 :(得分:0)

我认为你应该使用PARTIAL_WAKELOCK

改变这个:

wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "funguj");

到此:

wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "funguj");

按下锁定按钮link

后,这会使cpu保持打开状态

你在android manifest中设置了权限吗?

<uses-permission android:name="android.permission.WAKE_LOCK" />