这是一个非常奇怪的问题。我有一本书,我一直在阅读(Android Developer's Cookbook)
我正在制作一个非常基本的应用程序(至少,我认为它是基本的)
我遇到的问题是这个。当我创建的服务停止时,活动会完成服务的工作。
我想监听手机上的Z轴读数(总是在后台)
这是我的项目结构: TiltMonitorActivity TiltMonitorService
来自TiltMonitorActivity:
@Override
public void onClick(View v) {
boolean error = false;
if (tbService.isChecked()) {
try {
startService(backgroundService);
}
catch (Exception e) {
error = true;
}
finally {
if (error)
{
errorToast();
}
}
}
else if (!tbService.isChecked()) {
try {
stopService(backgroundService);
}
catch (Exception e){
error = true;
}
finally {
if (error)
{
errorToast();
}
}
}
}
});
我还把它放在onBackPressed(),onPause,onDestroy
上 finish();
我在启动/停止服务时创建的意图
final Intent backgroundService = new Intent(this, TiltMonitorService.class);
来自TiltMonitorService:
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.IBinder;
import android.os.Vibrator;
import android.widget.Toast;
public class TiltMonitorService extends Service {
private SensorManager sensorManager = null;
private Vibrator vibrator = null;
private float[] accData = new float[3];
// These are for manual config
private float FORWARD_THRESHOLD = 5f;
private float BACKWARD_THRESHOLD = -5f;
// These are for auto config
// The phone will face forward, so a positive Z Axis means user is leaning backwards
// and that a negative Z Axis the user is leaning forward
private float AUTO_FORWARD_THRESHOLD = -1.5f;
private float AUTO_BACKWARD_THRESHOLD = 3.5f;
public static TiltMonitorActivity MAIN_ACTIVITY = null;
public static void setMainActivity(TiltMonitorActivity activity)
{
MAIN_ACTIVITY = activity;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
sensorManager.registerListener( tiltListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
}
private SensorEventListener tiltListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
accData[0] = event.values[0];
accData[1] = event.values[1];
accData[2] = event.values[2];
checkData(accData);
}
};
private void checkData(float[] accData)
{
if ((accData[2] < AUTO_FORWARD_THRESHOLD) || (accData[2] > AUTO_BACKWARD_THRESHOLD))
{
vibrator.vibrate(100);
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
}
再一次,我在活动中点击ToggleButton时得到了服务已启动和停止的Toast。
但任务仍然存在。 (低于或高于设定阈值时振动)
当我打开任务杀手时,该服务未运行。只有活动是(杀死它会杀死振动)
我不确定要搜索哪些字词,我找不到任何有同样问题的人。我试图在Activity中只发布相关代码以避免混乱。该服务全部发布。如果需要更多,我会及时提出。
感谢你们给出的任何见解
答案 0 :(得分:2)
猜测,因为您在onCreate中注册了侦听器而未在onDestroy中取消注册
在服务中,即。