尝试制作一个简单的计步器。一切正常,直到屏幕熄灭。这就是我尝试将传感器事件移动到服务的原因,如
中所述how to keep application running in background? keep collecting data?
我尽我所能,但无法找到错误。 这是活动代码
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class Main extends Activity implements OnClickListener {
private Button btnStart, btnStop;
static TextView tvX, tvY, tvZ;
static boolean started = false;
protected static double x,y,z;
@SuppressLint("SdCardPath")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wL = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"My Tag");
wL.acquire();
startService(new Intent(this, Calculations.class));
tvX= (TextView)findViewById(R.id.steps);
tvY= (TextView)findViewById(R.id.dist);
tvZ= (TextView)findViewById(R.id.cals);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
}
static void run() {
if (started) {
tvX.setText(Double.toString(1.1));
tvY.setText(Double.toString(y));
tvZ.setText(Double.toString(z));
}
else {
tvX.setText(Double.toString(5.5));
tvY.setText(Double.toString(y));
tvZ.setText(Double.toString(z));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
btnStart.setEnabled(false);
btnStop.setEnabled(true);
started = true;
// run();
break;
case R.id.btnStop:
btnStart.setEnabled(true);
btnStop.setEnabled(false);
started = false;
//run();
break;
default:
break;
}
}
}
这是服务代码
import android.annotation.TargetApi;
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.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class Calculations extends Service implements SensorEventListener {
private float[] gravity = new float[3];
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private static final String TAG = "Calculations";
static boolean started = false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
super.onDestroy();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
final float alpha = 0.8f;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
Main.x = event.values[0] - gravity[0];
Main.y = event.values[1] - gravity[1];
Main.z = event.values[2] - gravity[2];
Main.run();
//}
}
}
任何帮助将不胜感激。 谢谢。
答案 0 :(得分:0)
如果您在清单文件中提到此服务,请检查..
android:Process=".AnyName"
请检查一下。如果不存在,请添加此项。它应该是工作