2

我想创建一个应用程序,当主线程仍在工作时,我的传感器在另一个线程中获取值。我尝试了另一个线程,但它没有工作。我尝试使用服务和 IntentService,但传感器在主线程之后启动。我目前的代码是这样的。

我的服务.java

public class MyService extends IntentService implements SensorEventListener 
{
    public MyService()
    {
        super("MyService");
    }

    Sensor mLight;
    SensorManager mSensorManager;
    float max;
    float currentLux;
    float currentProx;

    @Override
    public IBinder onBind(Intent intent) 
    {
        Log.d("Service", "On bind");
        return null;
    }

    @Override
    public void onCreate() 
    {
        Log.d("Service", "On create");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) 
    {
        if( event.sensor.getType() == Sensor.TYPE_LIGHT)
        {
            currentLux=(int) event.values[0];
            float perc=(float) ((currentLux*100)/max);
            Log.d("Light", String.valueOf(perc));
        }   
        else if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
        {
            currentProx=(int) event.values[0];
            Log.d("Proximity", String.valueOf(currentProx));
        }
    }

    @Override
    public void onDestroy() 
    {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
        Log.d("Service", "On stop");
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onStart(Intent intent, int startid) 
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        Log.d("Service", "On start");
    }

    @Override//here u should register sensor and write onStartCommand not onStart
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager
                .getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_FASTEST);
        mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        mSensorManager.registerListener(this, mSensorManager
                .getDefaultSensor(Sensor.TYPE_PROXIMITY),
                SensorManager.SENSOR_DELAY_FASTEST);
        max=mLight.getMaximumRange();
        Log.d("MAX=", String.valueOf(max));
        return Service.START_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) 
    {
    }

}

监听器.java

public class Listeners extends Activity implements OnClickListener 
{
     Button button0;
     Intent listenersIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listeners);
        lista= new ArrayList<Float>();
        button0 = (Button) findViewById(R.id.button0);
        button0.setOnClickListener(this);
        listenersIntent = new Intent(this, MyService.class);
        startService(listenersIntent);
        for (int i=0; i<999; i++)
            Log.d("Main", String.valueOf(i));
    }

    @Override
    public void onClick(View arg0) 
    {
        stopService(listenersIntent);
    }
}

我的问题是我总是在任何接近/光值之前得到 1-999,而我希望它们一起运行。你能帮我吗?

4

1 回答 1

-1

您从 onCreate 方法中启动服务。该方法作为服务初始化,在主线程上运行。这意味着该服务无法在您的 onCreate 运行的同时运行其初始化(更不用说记录 0-998 它非常非常快 - 从您的日志中需要 80 毫秒)。

此外,您正在注册听众。即使在您的服务启动后也会调用它们。

你不能像那样同步这两个动作。(并且没有足够的详细信息说明您为什么需要任何同步来为您指明正确的方向)

于 2013-10-01T15:16:49.970 回答