0

这是一个非常奇怪的问题。我有一本书,我一直在读(Android 开发者的食谱)

我正在制作一个非常基本的应用程序(至少,我认为它是基本的)

我遇到的问题是这个。当我创建的服务停止时,活动会完成服务的工作。

我想监控手机上的 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 中只发布相关代码以避免混乱。该服务已完整发布。如果需要更多,我会及时提出。

谢谢你们提供的任何见解

4

1 回答 1

2

猜测一下,因为您在 onCreate 中注册了侦听器,而没有在 onDestroy 中取消注册

在服务中,就是这样。

于 2010-12-26T05:10:55.347 回答