Android的传感器和线程 [英] Android sensors and thread

查看:262
本文介绍了Android的传感器和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序,我的传感器在另一个线程获取值,而主线程仍在工作。我试着用只是一个线程,但它并没有工作。我试着用服务和IntentService,但传感器的主线程后启动。我现在的code是这样的。

I want to create an application where my sensors get values in another thread while the main thread is still working. I tried with just another thread, but it didn't working. I tried with a service and a IntentService, but the sensors start after the main thread. My current code is this.

MyService.java

MyService.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) 
    {
    }

}

Listeners.java

Listeners.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之前的任何接近/光值,而我希望他们能一起运行。你能帮助我吗?

My problem is that i get always 1-999 before any proximity/light values, while i want them to run together. Can you help me please?

推荐答案

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

You start a service from within an onCreate method. That method, as the service initialization, run on the main thread. It means the service cannot run its initialization at the same time as your onCreate is running (not to mention that logging 0-998 it very very fast - 80ms from your log).

此外,您注册的听众。这些将被称为即使您的服务已启动。

Moreover, you are registering listeners. Those will be called even after your service started.

您不能同步这些行动2这样的。 (而且没有足够的细节,为什么你需要在所有的同步你指出正确的方向)

You cannot synchronize those 2 actions like that. (And there is not enough details as to why you need any synchronization at all to point you in the right direction)

这篇关于Android的传感器和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆